local k=false local function l(k,l,E)MasterControl:AddToPlayerMovement(Vector3.new(0,0,-CurrentThrottle))CurrentThrottle=(l==Enum.UserInputState.End or Deccelerating)and 0 or-1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))Accelerating=not(l==Enum.UserInputState.End)if l==Enum.UserInputState.End and Deccelerating then CurrentThrottle=1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))end end local function E(k,l,E)MasterControl:AddToPlayerMovement(Vector3.new(0,0,-CurrentThrottle))CurrentThrottle=(l==Enum.UserInputState.End or Accelerating)and 0 or 1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))Deccelerating=not(l==Enum.UserInputState.End)if l==Enum.UserInputState.End and Accelerating then CurrentThrottle=-1 MasterControl:AddToPlayerMovement(Vector3.new(0,0,CurrentThrottle))end end local function D(k,l,E)MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer,0,0))CurrentSteer=(l==Enum.UserInputState.End or TurningLeft)and 0 or 1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))TurningRight=not(l==Enum.UserInputState.End)if l==Enum.UserInputState.End and TurningLeft then CurrentSteer=-1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))end end local function v(k,l,E)MasterControl:AddToPlayerMovement(Vector3.new(-CurrentSteer,0,0))CurrentSteer=(l==Enum.UserInputState.End or TurningRight)and 0 or-1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))TurningLeft=not(l==Enum.UserInputState.End)if l==Enum.UserInputState.End and TurningRight then CurrentSteer=1 MasterControl:AddToPlayerMovement(Vector3.new(CurrentSteer,0,0))end end local function g()local k=LocalPlayer and LocalPlayer.Character if k then for k,l in pairs(k:GetChildren())do if l:IsA("\072\117\109\097\110\111\105\100")then return l end end end end local function o(k)if k>.5 then return 1 elseif k<-0.5 then return-1 end return 0 end local function m()if CurrentVehicleSeat then local l=MasterControl:GetMoveVector()local E=false E=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(k and useTriggersForThrottle)then CurrentVehicleSeat.ThrottleFloat=-CurrentThrottle else CurrentVehicleSeat.ThrottleFloat=-l.z end CurrentVehicleSeat.SteerFloat=l.x end)if E==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(k and useTriggersForThrottle)then CurrentVehicleSeat.Throttle=-CurrentThrottle else CurrentVehicleSeat.Throttle=o(-l.z)end CurrentVehicleSeat.Steer=o(l.x)end end end local function s(k,g)if k then if g and g:IsA("\086\101\104\105\099\108\101\083\101\097\116")then CurrentVehicleSeat=g if useTriggersForThrottle then ContextActionService:BindAction("\116\104\114\111\116\116\108\101\065\099\099\101\108",l,false,Enum.KeyCode.ButtonR2)ContextActionService:BindAction("\116\104\114\111\116\116\108\101\068\101\099\099\101\108",E,false,Enum.KeyCode.ButtonL2)end ContextActionService:BindAction("\097\114\114\111\119\083\116\101\101\114\082\105\103\104\116",D,false,Enum.KeyCode.Right)ContextActionService:BindAction("\097\114\114\111\119\083\116\101\101\114\076\101\102\116",v,false,Enum.KeyCode.Left)local k=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,m)end)if not k then if RenderSteppedCn then return end RenderSteppedCn=RunService.RenderStepped:connect(m)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 k=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 k and RenderSteppedCn then RenderSteppedCn:disconnect()RenderSteppedCn=nil end end end local function b(k)local l=g()while not l do task.wait()l=g()end if HumanoidSeatedCn then HumanoidSeatedCn:disconnect()HumanoidSeatedCn=nil end HumanoidSeatedCn=l.Seated:connect(s)end if LocalPlayer.Character then b(LocalPlayer.Character)end LocalPlayer.CharacterAdded:connect(b)return VehicleController

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
		task.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
