local a=false local function w(a,w,j)MasterControl:AddToPlayerMovement(Vector3.new(0,0,-CurrentThrottle))CurrentThrottle=(w==Enum.UserInputState.End or Deccelerating)and 0 or-1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))Accelerating=not(w==Enum.UserInputState.End)if w==Enum.UserInputState.End and Deccelerating then CurrentThrottle=1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))end end local function j(a,w,j)MasterControl:AddToPlayerMovement(Vector3.new(0,0,-CurrentThrottle))CurrentThrottle=(w==Enum.UserInputState.End or Accelerating)and 0 or 1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))Deccelerating=not(w==Enum.UserInputState.End)if w==Enum.UserInputState.End and Accelerating then CurrentThrottle=-1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))end end local function O()local a=LocalPlayer and LocalPlayer.Character if a then for a,w in pairs(a:GetChildren())do if w:IsA("\072\117\109\097\110\111\105\100")then return w end end end end local function T(a)if a>.5 then return 1 elseif a<-0.5 then return-1 end return 0 end local function H()if CurrentVehicleSeat then local w=MasterControl:GetMoveVector()if(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetGamepadConnected(Enum.UserInputType.Gamepad1)and(a and useTriggersForThrottle)then CurrentVehicleSeat.Throttle=-CurrentThrottle else CurrentVehicleSeat.Throttle=T(-w.z)end CurrentVehicleSeat.Steer=T(w.x)end end local function n(a,O)if a then if O and O:IsA("\086\101\104\105\099\108\101\083\101\097\116")then CurrentVehicleSeat=O if useTriggersForThrottle then ContextActionService:BindAction("\116\104\114\111\116\116\108\101\065\099\099\101\108",w,false,Enum.KeyCode.ButtonR2)ContextActionService:BindAction("\116\104\114\111\116\116\108\101\068\101\099\099\101\108",j,false,Enum.KeyCode.ButtonL2)end local a=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,H)end)if not a then if RenderSteppedCn then return end RenderSteppedCn=RunService.RenderStepped:connect(H)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 MasterControl:AddToPlayerMovement(Vector3.new(0,0,-CurrentThrottle))CurrentThrottle=0 local a=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 a and RenderSteppedCn then RenderSteppedCn:disconnect()RenderSteppedCn=nil end end end local function C(a)local w=O()while not w do wait()w=O()end if HumanoidSeatedCn then HumanoidSeatedCn:disconnect()HumanoidSeatedCn=nil end HumanoidSeatedCn=w.Seated:connect(n)end if LocalPlayer.Character then C(LocalPlayer.Character)end LocalPlayer.CharacterAdded:connect(C)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 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()
		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

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
			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
		MasterControl:AddToPlayerMovement(Vector3.new(0, 0, -CurrentThrottle))
		CurrentThrottle = 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
