Script isnt detecting values changed by vehicleseats?

Hi,
I am using a VehicleSeat to detect player input which is forwarded to a NumberValue but only if Occupant isn’t equal to nil. The reason why I am using the NumberValue is because there are two vehicleseats, and I only want the system to detect one input

The problem is that the script does not identify when the value changes, with a while true do loop.

I can’t find a solution, here is the script for the vehicleseat detecting the value:

while true do
	wait(.01)
	if script.Parent.Occupant ~= nil then
		script.Parent.Parent.Dir.Value = script.Parent.ThrottleFloat
	end
end

This is the script that is supposed to take action on the value changing:

direction = script.Parent.Parent.Parent.Dir.Value
MainPart = script.Parent
hinge = script.Parent.HingeConstraint
while true do
	wait(.01)
	if direction == 1 then
		MainPart.Power.Disabled = false
		MainPart.Brake.Disabled = true
		hinge.MotorMaxAcceleration = 5
	end
	if direction == -1 then
		MainPart.Power.Disabled = true
		MainPart.Brake.Disabled = false
		hinge.MotorMaxAcceleration = 8
	end
	if direction == 0 then
		MainPart.Power.Disabled = true
		MainPart.Brake.Disabled = true
		hinge.MotorMaxAcceleration = 0
	end
end

Sorry if the concept is difficult to understand

Your error is you’re only getting the direction from the NumberValue once at the beginning of the script. Numbers are immutable which means that if the NumberValue’s Value changes, direction won’t change with it; direction would never change.

Your solution? Instead of saving the direction to a value beforehand, get and use the actual value when checking the direction in the while-loop.

Some other things you might want to consider: since you’re comparing the values to integers, instead of using script.Parent.ThrottleFloat, you could just use script.Parent.Throttle.

Also, while-loops are a lot less efficient and optimal than events. Possible events you may be interested in include IntValue.Changed (an IntValue would be more appropriate than a NumberValue since Throttle is an integer and you’re checking it against integers), which fires when a property (such as Value) changes. Then it won’t be running 100 times a second regardless of whether the direction changed or not.

Oh yes, I didn’t realise that because I quickly did find and replace on my script. Also I am looking into using Changed in my script to make it less laggier.