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

Script won't detect change in value?

So, I've been stuck with this small bit of code, which is vital for making a driving system work for me. When the value is changed, the code is meant to detect that change depending on the number the value holds. I've also noticed on load that the first while statement runs fine. Anyways here's the code:

while script.Parent.Accel.Value==0 do
	wait(.5)
	script.Parent.Parent.VehicleSeat.Torque=0
	break
end

while script.Parent.Accel.Value==10 do
	wait(.5)
	script.Parent.Parent.VehicleSeat.Torque=.1
	break
end

I have no idea how this isn't working as in theory it should. Any help would be greatly appreciated. Cheers!

EDIT -- I noticed i messed up a little bit of the code, now i know that it doesn't work as i've trialed with it corrected. Sorry for any confusion!

1 comment

1

You may want to look into the "Changed" event for Values whether it is an NumberValue, IntValue, or any other value. When the event fires, the event handler will then check if the value is equal to 0. Also, you are setting the Torque twice, for 0 to .1 every time the value is 0.

1 answer

Your first while loop runs once if the value is initially 0 and it sets the torque to 0. It immediately stops itself after the first iteration. The second while loop has the same condition and changes the torque back to .1.

while condition do
	-- code
	break
end

is equivalent to

if condition then
	-- code
end

because the loop can only run once. Since you're talking about changes, it makes sense to listen to the Changed event:

script.Parent.Accel.Changed:Connect(function(newValue)
	if newValue == 0 then
		script.Parent.Parent.VehicleSeat.Torque = .1
	else
		script.Parent.Parent.VehicleSeat.Torque = 0
	end
end)

I can't tell if this is quite what you want to do. You say in theory your code should work but I have no idea what you think it's supposed to do.

2 comments

0

I think i was a tad vague in my explanation, so what im trying to achieve is the higher the value, the higher the torque. Its for a train so its basically 0 is 0% throttle, 10 is 10% throttle and so on and i need it to be able to go between each value and run the code every time said' value is used.

0

Ah, I've noticed an error that would have confused most

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