local u=false local function q(u,q,h)MasterControl:AddToPlayerMovement(Vector3.new(0,0,-CurrentThrottle))CurrentThrottle=(q==Enum.UserInputState.End or Deccelerating)and 0 or-1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))Accelerating=not(q==Enum.UserInputState.End)if q==Enum.UserInputState.End and Deccelerating then CurrentThrottle=1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))end end local function h(u,q,h)MasterControl:AddToPlayerMovement(Vector3.new(0,0,-CurrentThrottle))CurrentThrottle=(q==Enum.UserInputState.End or Accelerating)and 0 or 1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))Deccelerating=not(q==Enum.UserInputState.End)if q==Enum.UserInputState.End and Accelerating then CurrentThrottle=-1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))end end local function S(u,q,h)MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer,0,0))CurrentSteer=(q==Enum.UserInputState.End or TurningLeft)and 0 or 1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))TurningRight=not(q==Enum.UserInputState.End)if q==Enum.UserInputState.End and TurningLeft then CurrentSteer=-1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))end end local function Y(u,q,h)MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer,0,0))CurrentSteer=(q==Enum.UserInputState.End or TurningRight)and 0 or-1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))TurningLeft=not(q==Enum.UserInputState.End)if q==Enum.UserInputState.End and TurningRight then CurrentSteer=1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))end end local function p()local u=LocalPlayer and LocalPlayer.Character if u then for u,q in pairs(u:GetChildren())do if q:IsA("\072\117\109\097\110\111\105\100")then return q end end end end local function t(u)if u>.5 then return 1 elseif u<-0.5 then return-1 end return 0 end local function e()if CurrentVehicleSeat then local q=MasterControl:GetMoveVector()local h=false h=pcall(function()if(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetGamepadConnected(Enum.UserInputType.Gamepad1)and(u and useTriggersForThrottle)then CurrentVehicleSeat.ThrottleFloat=-CurrentThrottle else CurrentVehicleSeat.ThrottleFloat=-q.z end CurrentVehicleSeat.SteerFloat=q.x end)if h==false then if(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetGamepadConnected(Enum.UserInputType.Gamepad1)and(u and useTriggersForThrottle)then CurrentVehicleSeat.Throttle=-CurrentThrottle else CurrentVehicleSeat.Throttle=t(-q.z)end CurrentVehicleSeat.Steer=t(q.x)end end end local function j(u,p)if u then if p and p:IsA("\086\101\104\105\099\108\101\083\101\097\116")then CurrentVehicleSeat=p if useTriggersForThrottle then ContextActionService:BindAction("\116\104\114\111\116\116\108\101\065\099\099\101\108",q,false,Enum.KeyCode.ButtonR2)ContextActionService:BindAction("\116\104\114\111\116\116\108\101\068\101\099\099\101\108",h,false,Enum.KeyCode.ButtonL2)end ContextActionService:BindAction("\097\114\114\111\119\083\116\101\101\114\082\105\103\104\116",S,false,Enum.KeyCode.Right)ContextActionService:BindAction("\097\114\114\111\119\083\116\101\101\114\076\101\102\116",Y,false,Enum.KeyCode.Left)local u=pcall(function()RunService:BindToRenderStep("\086\101\104\105\099\108\101\067\111\110\116\114\111\108\083\116\101\112",Enum.RenderPriority.Input.Value,e)end)if not u then if RenderSteppedCn then return end RenderSteppedCn=RunService.RenderStepped:connect(e)end end else CurrentVehicleSeat=nil if useTriggersForThrottle then ContextActionService:UnbindAction("\116\104\114\111\116\116\108\101\065\099\099\101\108")ContextActionService:UnbindAction("\116\104\114\111\116\116\108\101\068\101\099\099\101\108")end ContextActionService:UnbindAction("\097\114\114\111\119\083\116\101\101\114\082\105\103\104\116")ContextActionService:UnbindAction("\097\114\114\111\119\083\116\101\101\114\076\101\102\116")MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer,0,-CurrentThrottle))CurrentThrottle=0 CurrentSteer=0 local u=pcall(function()RunService:UnbindFromRenderStep("\086\101\104\105\099\108\101\067\111\110\116\114\111\108\083\116\101\112")end)if not u and RenderSteppedCn then RenderSteppedCn:disconnect()RenderSteppedCn=nil end end end local function J(u)local q=p()while not q do wait()q=p()end if HumanoidSeatedCn then HumanoidSeatedCn:disconnect()HumanoidSeatedCn=nil end HumanoidSeatedCn=q.Seated:connect(j)end if LocalPlayer.Character then J(LocalPlayer.Character)end LocalPlayer.CharacterAdded:connect(J)return VehicleController
-- Also set this to true if you want the thumbstick to not affect throttle, only triggers when a gamepad is conected
local onlyTriggersForThrottle = false

local function onThrottleAccel(actionName, inputState, inputObject)
	MasterControl:AddToPlayerMovement(Vector3.new(0, 0, -CurrentThrottle))
	CurrentThrottle = (inputState == Enum.UserInputState.End or Deccelerating) and 0 or -1
	MasterControl:AddToPlayerMovement(Vector3.new(0, 0, CurrentThrottle))
	Accelerating = not (inputState == Enum.UserInputState.End)
	if (inputState == Enum.UserInputState.End) and Deccelerating then
		CurrentThrottle = 1
		MasterControl:AddToPlayerMovement(Vector3.new(0, 0, CurrentThrottle))
	end
end

local function onThrottleDeccel(actionName, inputState, inputObject)
	MasterControl:AddToPlayerMovement(Vector3.new(0, 0, -CurrentThrottle))
	CurrentThrottle = (inputState == Enum.UserInputState.End or Accelerating) and 0 or 1
	MasterControl:AddToPlayerMovement(Vector3.new(0, 0, CurrentThrottle))
	Deccelerating = not (inputState == Enum.UserInputState.End)
	if (inputState == Enum.UserInputState.End) and Accelerating then
		CurrentThrottle = -1
		MasterControl:AddToPlayerMovement(Vector3.new(0, 0, CurrentThrottle))
	end
end

local function onSteerRight(actionName, inputState, inputObject)
	MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer, 0, 0))
	CurrentSteer = (inputState == Enum.UserInputState.End or TurningLeft) and 0 or 1
	MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer, 0, 0))
	TurningRight = not (inputState == Enum.UserInputState.End)
	if (inputState == Enum.UserInputState.End) and TurningLeft then
		CurrentSteer = -1
		MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer, 0, 0))
	end
end

local function onSteerLeft(actionName, inputState, inputObject)
	MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer, 0, 0))
	CurrentSteer = (inputState == Enum.UserInputState.End or TurningRight) and 0 or -1
	MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer, 0, 0))
	TurningLeft = not (inputState == Enum.UserInputState.End)
	if (inputState == Enum.UserInputState.End) and TurningRight then
		CurrentSteer = 1
		MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer, 0, 0))
	end
end

local function getHumanoid()
	local character = LocalPlayer and LocalPlayer.Character
	if character then
		for _, child in pairs(character:GetChildren()) do
			if child:IsA("Humanoid") then
				return child
			end
		end
	end
end

local function getClosestFittingValue(value)
	if value > 0.5 then
		return 1
	elseif value < -0.5 then
		return -1
	end
	return 0
end

local function onRenderStepped()
	if CurrentVehicleSeat then
		local moveValue = MasterControl:GetMoveVector()
		local didSetThrottleSteerFloat = false
		didSetThrottleSteerFloat = pcall(function()
			if
				game:GetService("UserInputService"):GetGamepadConnected(Enum.UserInputType.Gamepad1)
				and onlyTriggersForThrottle
				and useTriggersForThrottle
			then
				CurrentVehicleSeat.ThrottleFloat = -CurrentThrottle
			else
				CurrentVehicleSeat.ThrottleFloat = -moveValue.z
			end
			CurrentVehicleSeat.SteerFloat = moveValue.x
		end)

		if didSetThrottleSteerFloat == false then
			if
				game:GetService("UserInputService"):GetGamepadConnected(Enum.UserInputType.Gamepad1)
				and onlyTriggersForThrottle
				and useTriggersForThrottle
			then
				CurrentVehicleSeat.Throttle = -CurrentThrottle
			else
				CurrentVehicleSeat.Throttle = getClosestFittingValue(-moveValue.z)
			end
			CurrentVehicleSeat.Steer = getClosestFittingValue(moveValue.x)
		end
	end
end

local function onSeated(active, currentSeatPart)
	if active then
		if currentSeatPart and currentSeatPart:IsA("VehicleSeat") then
			CurrentVehicleSeat = currentSeatPart
			if useTriggersForThrottle then
				ContextActionService:BindAction("throttleAccel", onThrottleAccel, false, Enum.KeyCode.ButtonR2)
				ContextActionService:BindAction("throttleDeccel", onThrottleDeccel, false, Enum.KeyCode.ButtonL2)
			end
			ContextActionService:BindAction("arrowSteerRight", onSteerRight, false, Enum.KeyCode.Right)
			ContextActionService:BindAction("arrowSteerLeft", onSteerLeft, false, Enum.KeyCode.Left)
			local success = pcall(function()
				RunService:BindToRenderStep("VehicleControlStep", Enum.RenderPriority.Input.Value, onRenderStepped)
			end)

			if not success then
				if RenderSteppedCn then
					return
				end
				RenderSteppedCn = RunService.RenderStepped:connect(onRenderStepped)
			end
		end
	else
		CurrentVehicleSeat = nil
		if useTriggersForThrottle then
			ContextActionService:UnbindAction("throttleAccel")
			ContextActionService:UnbindAction("throttleDeccel")
		end
		ContextActionService:UnbindAction("arrowSteerRight")
		ContextActionService:UnbindAction("arrowSteerLeft")
		MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer, 0, -CurrentThrottle))
		CurrentThrottle = 0
		CurrentSteer = 0
		local success = pcall(function()
			RunService:UnbindFromRenderStep("VehicleControlStep")
		end)
		if not success and RenderSteppedCn then
			RenderSteppedCn:disconnect()
			RenderSteppedCn = nil
		end
	end
end

local function CharacterAdded(character)
	local humanoid = getHumanoid()
	while not humanoid do
		wait()
		humanoid = getHumanoid()
	end
	--
	if HumanoidSeatedCn then
		HumanoidSeatedCn:disconnect()
		HumanoidSeatedCn = nil
	end
	HumanoidSeatedCn = humanoid.Seated:connect(onSeated)
end

if LocalPlayer.Character then
	CharacterAdded(LocalPlayer.Character)
end
LocalPlayer.CharacterAdded:connect(CharacterAdded)

return VehicleController
