What is causing input lag on this vehicle script?

I followed a tutorial on the roblox developer hub and for some reason there is input lag (Whenever I press a key, it can anywhere, to a second for the vehicle to move.)

local Parent = script.Parent
local vehicleSeat = Parent.Base.VehicleSeat
local A = Parent.WheelBL.WheelMount.BL
local B = Parent.WheelBR.WheelMount.BR
local C = Parent.WheelFL.WheelMount.FL
local D = Parent.WheelFR.WheelMount.FR

local motorFR = Parent.WheelFR:FindFirstChildWhichIsA("CylindricalConstraint", true)
local motorFL = Parent.WheelFL:FindFirstChildWhichIsA("CylindricalConstraint", true)
local motorBR = Parent.WheelBR:FindFirstChildWhichIsA("CylindricalConstraint", true)
local motorBL = Parent.WheelBL:FindFirstChildWhichIsA("CylindricalConstraint", true)

local springFR = Parent.WheelFR:FindFirstChildWhichIsA("SpringConstraint", true)
local springFL = Parent.WheelFL:FindFirstChildWhichIsA("SpringConstraint", true)
local springBR = Parent.WheelBR:FindFirstChildWhichIsA("SpringConstraint", true)
local springBL = Parent.WheelBL:FindFirstChildWhichIsA("SpringConstraint", true)

local wheelHingeR = Parent.WheelFR:FindFirstChildWhichIsA("HingeConstraint", true)
local wheelHingeL = Parent.WheelFL:FindFirstChildWhichIsA("HingeConstraint", true)


-- TUNING VALUES
----------------------------------------
-- Factor of torque applied to get the wheels spinning
-- Larger number generally means faster acceleration
local TORQUE = 10000 

-- Factor of torque applied to change the wheel direction
-- Larger number generally means faster braking
local BRAKING_TORQUE = 8000

-- Max angle the wheels will reach when turning
-- Higher number means sharper turning, but too high means the wheels might hit the car base
local MAX_TURN_ANGLE = 20

-- Car max speed
local MAX_SPEED = 100
----------------------------------------


-- HELPER FUNCTIONS
----------------------------------------
-- Set the "MotorMaxTorque" property on all of the CylindricalConstraint motors
local function setMotorTorque(torque)
	motorFR.MotorMaxTorque = torque
	motorFL.MotorMaxTorque = torque
	motorBR.MotorMaxTorque = torque
	motorBL.MotorMaxTorque = torque
end

-- Set the "AngularVelocity" property on all of the CylindricalConstraint motors
local function setMotorVelocity(vel)
	motorFL.AngularVelocity = vel
	motorBL.AngularVelocity = vel
	-- Motors on the right side are facing the opposite direction, so negative velocity must be used
	motorFR.AngularVelocity = -vel
	motorBR.AngularVelocity = -vel
end

-- Calculate the average linear velocity of the car based on the rate at which all wheels are spinning
local function getAverageVelocity()
	local vFR = -motorFR.Attachment1.WorldAxis:Dot(motorFR.Attachment1.Parent.RotVelocity)
	local vRR = -motorBR.Attachment1.WorldAxis:Dot(motorBR.Attachment1.Parent.RotVelocity)
	local vFL = motorFL.Attachment1.WorldAxis:Dot(motorFL.Attachment1.Parent.RotVelocity)
	local vRL = motorBL.Attachment1.WorldAxis:Dot(motorBL.Attachment1.Parent.RotVelocity)
	return 0.25 * ( vFR + vFL + vRR + vRL )
end


-- DRIVE LOOP
----------------------------------------
while true do

	-- Input values taken from the VehicleSeat
	local steerFloat = vehicleSeat.SteerFloat  -- Forward and backward direction, between -1 and 1
	local throttle = vehicleSeat.ThrottleFloat  -- Left and right direction, between -1 and 1

	-- Convert "steerFloat" to an angle for the HingeConstraint servos
	local turnAngle = steerFloat * MAX_TURN_ANGLE
	wheelHingeR.TargetAngle = turnAngle
	wheelHingeL.TargetAngle = turnAngle

	-- Apply torque to the CylindricalConstraint motors depending on our throttle input and the current speed of the car
	local currentVel = getAverageVelocity()
	local targetVel = 0
	local motorTorque = 0

	-- Idling
	if math.abs(throttle) < 0.1 then
		motorTorque = 5

	-- Accelerating
	elseif math.abs(throttle * currentVel) > 0 then
		A.MotorMaxAngularAcceleration = 20
		B.MotorMaxAngularAcceleration = 20
		C.MotorMaxAngularAcceleration = 20
		D.MotorMaxAngularAcceleration = 20
		-- Reduce torque with speed (if torque was constant, there would be a jerk reaching the target velocity)
		-- This also produces a reduction in speed when turning
		local r = math.abs(currentVel) / MAX_SPEED
		-- Torque should be more sensitive to input at low throttle than high, so square the "throttle" value
		motorTorque = math.exp( - 3 * r * r ) * TORQUE * throttle * throttle
		targetVel = math.sign(throttle) * 10000  -- Arbitrary large number

	-- Braking
	else
		A.MotorMaxAngularAcceleration = 10
		B.MotorMaxAngularAcceleration = 10
		C.MotorMaxAngularAcceleration = 10
		D.MotorMaxAngularAcceleration = 10
		motorTorque = BRAKING_TORQUE * throttle * throttle
	end

	-- Use helper functions to apply torque and target velocity to all motors
	setMotorTorque(motorTorque)
	setMotorVelocity(targetVel)
	wait()
end
1 Like

Every time that while wait() do is run, your doing complex math and functions. This will create I put lag because it’s now doing all this stuff before the vehicle moves. Your only option really is too optimize the code.

1 Like

This may be because of network latency. Have you tried setting the network owner of the vehicle to the driver and handling the vehicle on the client? This will give the driver instant feedback.

2 Likes

If this is a local script, you’re right to use WaitForChild on the elements you need to reference.

However, waiting for a large amount of children could cause some lag toward the start of the script.

1 Like