Car.AncestryChanged:Connect(function()if not Car:IsDescendantOf(Workspace)then unbindActions()LocalVehicleSeating.ExitSeat()LocalVehicleSeating.DisconnectFromSeatExitEvent(onExitSeat)print("\099\097\114 \114\101\109\111\118\101\100 \102\114\111\109 \119\111\114\107\115\112\097\099\101")script.Disabled=true end end)local function B(B,v)for B,C in ipairs(B)do if v.KeyCode==C.KeyCode then if C.Axis then local B=v.Position*C.Axis local l=math.sign((B.X+B.Y)+B.Z)return B.magnitude*l else return 1 end end end return 0 end local function v(B,v,C)if v==Enum.UserInputState.Begin then LocalVehicleSeating.ExitSeat()end end ContextActionService:BindAction(EXIT_ACTION_NAME,v,false,Keymap.EnterVehicleGamepad,Keymap.EnterVehicleKeyboard)local function C(v,C,l)if C==Enum.UserInputState.Begin or C==Enum.UserInputState.Change then SteerLeftValue=B(Keymap.SteerLeft,l)else SteerLeftValue=0 end return Enum.ContextActionResult.Pass end ContextActionService:BindActionAtPriority(STEER_LEFT_ACTION_NAME,C,false,Enum.ContextActionPriority.High.Value,unpack(Keymap.KeysForAction("\083\116\101\101\114\076\101\102\116")))local function l(v,C,l)if C==Enum.UserInputState.Begin or C==Enum.UserInputState.Change then SteerRightValue=B(Keymap.SteerRight,l)else SteerRightValue=0 end return Enum.ContextActionResult.Pass end ContextActionService:BindActionAtPriority(STEER_RIGHT_ACTION_NAME,l,false,Enum.ContextActionPriority.High.Value,unpack(Keymap.KeysForAction("\083\116\101\101\114\082\105\103\104\116")))local function T(v,C,l)if C==Enum.UserInputState.Begin or C==Enum.UserInputState.Change then ThrottleValue=B(Keymap.Throttle,l)else ThrottleValue=0 end end ContextActionService:BindAction(THROTTLE_ACTION_NAME,T,false,unpack(Keymap.KeysForAction("\084\104\114\111\116\116\108\101")))local function W(v,C,l)if C==Enum.UserInputState.Begin or C==Enum.UserInputState.Change then BrakeValue=B(Keymap.Brake,l)else BrakeValue=0 end end ContextActionService:BindAction(BRAKE_ACTION_NAME,W,false,unpack(Keymap.KeysForAction("\066\114\097\107\101")))local function s(B,v,C)if v==Enum.UserInputState.Begin then HandBrakeValue=1 else HandBrakeValue=0 end end ContextActionService:BindAction(HAND_BRAKE_ACTION_NAME,s,false,unpack(Keymap.KeysForAction("\072\097\110\100\098\114\097\107\101")))

Car.AncestryChanged:Connect(function()
	if not Car:IsDescendantOf(Workspace) then
		unbindActions()
		
		LocalVehicleSeating.ExitSeat()
		LocalVehicleSeating.DisconnectFromSeatExitEvent(onExitSeat)
		-- stop seated anim
		print("car removed from workspace")
		
		script.Disabled = true
	end
end)

local function getInputValue(keyInfo, inputObj)
	for _, keyCodeInfo in ipairs(keyInfo) do
		if inputObj.KeyCode == keyCodeInfo.KeyCode then
			if keyCodeInfo.Axis then
				local inputVector = inputObj.Position * keyCodeInfo.Axis
				local sign = math.sign(inputVector.X + inputVector.Y + inputVector.Z)
				return inputVector.magnitude * sign
			else
				return 1
			end
		end
	end
	return 0
end

local function exitVehicle(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		LocalVehicleSeating.ExitSeat()
		-- stop seated anim
	end
end

ContextActionService:BindAction(
	EXIT_ACTION_NAME,
	exitVehicle,
	false,
	Keymap.EnterVehicleGamepad,
	Keymap.EnterVehicleKeyboard
)

local function steerLeftAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		SteerLeftValue = getInputValue(Keymap.SteerLeft, inputObj)
	else
		SteerLeftValue = 0
	end
	return Enum.ContextActionResult.Pass
end

ContextActionService:BindActionAtPriority(
	STEER_LEFT_ACTION_NAME,
	steerLeftAction,
	false,
	Enum.ContextActionPriority.High.Value,
	unpack(Keymap.KeysForAction("SteerLeft"))
)

local function steerRightAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		SteerRightValue = getInputValue(Keymap.SteerRight, inputObj)
	else
		SteerRightValue = 0
	end
	return Enum.ContextActionResult.Pass
end

ContextActionService:BindActionAtPriority(
	STEER_RIGHT_ACTION_NAME,
	steerRightAction,
	false,
	Enum.ContextActionPriority.High.Value,
	unpack(Keymap.KeysForAction("SteerRight"))
)

local function throttleAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		ThrottleValue = getInputValue(Keymap.Throttle, inputObj)
	else
		ThrottleValue = 0
	end
end

ContextActionService:BindAction(
	THROTTLE_ACTION_NAME,
	throttleAction,
	false,
	unpack(Keymap.KeysForAction("Throttle"))
)

local function brakeAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		BrakeValue = getInputValue(Keymap.Brake, inputObj)
	else
		BrakeValue = 0
	end
end

ContextActionService:BindAction(
	BRAKE_ACTION_NAME,
	brakeAction,
	false,
	unpack(Keymap.KeysForAction("Brake"))
)

local function handBrakeAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		HandBrakeValue = 1
	else
		HandBrakeValue = 0
	end
end

ContextActionService:BindAction(
	HAND_BRAKE_ACTION_NAME,
	handBrakeAction,
	false,
	unpack(Keymap.KeysForAction("Handbrake"))
)
