Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script won't detect change in value?

Asked by 5 years ago
Edited 5 years ago

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
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. Stephenthefox 94 — 5y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
5 years ago

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.

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. XxHURSTY 0 — 5y
0
Ah, I've noticed an error that would have confused most XxHURSTY 0 — 5y
Ad

Answer this question