Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a localscript steering work in a specific vehicle?

Alright, so I made a localscript inside of StarterPlayerScripts because according to some resources, a localscript can stop the input lag of the cars physics. Now my problem is that how am I gonna call a specific VehicleSeat that the player is sitting on and what if there were 2 players and there were 2 models of the same name?

LocalScript:

--car and seat variables
local seat = workspace.Chassis.VehicleSeat
local car = seat.Parent
--constraint variables
local LS = car.FL.SpringMount.Steer
local RS = car.FR.SpringMount.Steer
--VehicleSeat function
local function onChanged(property)
	--forward and reverse speed
	if seat.Throttle >= 0 then
		seat.Speed.Value = 50
	else
		seat.Speed.Value = 20
	end
	--steering
	if seat.Steer == -1 then
		LS.AngularSpeed = 1
		RS.AngularSpeed = 0.8
		LS.TargetAngle = -30
		RS.TargetAngle = -25
	end
	if seat.Steer == 1 then
		LS.AngularSpeed = 0.8
		RS.AngularSpeed = 1
		LS.TargetAngle = 25
		RS.TargetAngle = 30
	end
	if seat.Steer == 0 then
		LS.AngularSpeed = 5
		RS.AngularSpeed = 5
		LS.TargetAngle = 0
		RS.TargetAngle = 0
	end
end
seat.Changed:Connect(onChanged)

1 answer

To fix it, you should instead detect when the characters humanoids event "Seated" has fired. And put all the cars under some folder in workspace (named Cars)

localscript for detecting player sitting (under StarterPlayer->StarterCharacterScripts):


local chr = script.Parent
local humanoid = chr:WaitForChild("Humanoid")
local carFolder = workspace:WaitForChild("Cars")

local seat,car
local conn -- Event connection for seat.changed

local LS,RS

local function onChanged() end -- Put YOUR onChanged code here

humanoid.Seated:Connect(function(active, seatPart)
	if not seatPart:IsDescendantOf(carFolder) then
		return nil
	end -- Check if the seat is apart of any car under the "Cars" folder, if not then stop function
	
	if not active then
		conn:Disconnect()
		return nil
	end -- Active says whether they sat down or got up (false meaning stopped sitting)
	seat,car = seatPart, seatPart.Parent
	LS = car:WaitForChild("FL"):WaitForChild("SprintMount"):WaitForChild("Steer")
	RS = car:WaitForChild("FR"):WaitForChild("SprintMount"):WaitForChild("Steer")
	conn = seat.Changed:Connect(onChanged)
end)

I hope this helps.

4 comments

0

It gave me an error saying Infinite yield possible on 'Players.Gam3r_Play3r.PlayerScripts:WaitForChild("Humanoid")'

0

I moved it to StarterCharacterScripts then it gave me another error saying Infinite yield possible on 'Workspace.Cars.Chassis.FL:WaitForChild("SprintMount")'

0

Is there a way to reduce steering latency without localscripts?

0

^ No there isn't

This discussion is preserved as it was. New answers, comments, and votes are closed.