Hello, My name is OfficerJorden139. I need some help for a project I'm doing. I'm using a loop sequence for my RP stove. But when I click the button that is connected to the script the stove turns on but when I click it again it doesn't remove the loop. Can you please help me?? Here's the code
Plate = script.Parent.Parent.Plate isOn = false function on() isOn = true while true do wait(1) Plate.BrickColor = BrickColor.new("Persimmon") wait(1) Plate.BrickColor = BrickColor.new("Terra Cotta") end end function off() isOn = false Plate.BrickColor = BrickColor.new("Medium stone grey") end function run() if isOn == false then on() else off() end end script.Parent.ClickDetector.MouseClick:connect(run)
Can you please help me?
The reason it doesn't remove the loop is that you never check in the loop if it should shut off.
The simplest method to fix this is to simply change the while true do
to while isOn do
, however, because this could happen during either of those two wait()
s within the loop, this will likely make the color stick to an 'active' color.
My preferred method to deal with this actually puts the loop outside of the function:
Plate = script.Parent.Parent.Plate isOn = false colorOn1 = BrickColor.new("Persimmon") colorOn2 = BrickColor.new("Terra Cotta") colorOff = BrickColor.new("Medium stone grey") function run() isOn = not isOn -- if false, set to true else if true, set to false end script.Parent.ClickDetector.MouseClick:connect(run) while true do if isOn then if Plate.BrickColor = = colorOn2 then Plate.BrickColor = colorOn1 else Plate.BrickColor = colorOn2 end wait(1) else Plate.BrickColor = colorOff wait() end end
This isn't the most efficient way to do this, but it's simpler to read than a more efficient method that avoids the unnecessary loop safely.
the easiest way in my opinion is to simply put
if isOn.Value == false then break end
inside the loop like so:
Plate = script.Parent.Parent.Plate isOn = false function on() isOn = true while true do if isOn.Value == false then break end wait(1) Plate.BrickColor = BrickColor.new("Persimmon") if isOn.Value == false then break end wait(1) Plate.BrickColor = BrickColor.new("Terra Cotta") end end function off() isOn = false Plate.BrickColor = BrickColor.new("Medium stone grey") end function run() if isOn == false then on() else off() end end script.Parent.ClickDetector.MouseClick:connect(run)
this will make the loop stop when it finds that isOn is not true (and it accomodates for the wait(1) that shows up twice)
The ClickDetector Event Runs the script each time you click right ? so lets say you clicked forthe first time .. the script will run is on will be false and the on() function will start working .
when you click again . the clickDetector event will run the script again meaning that the value that was set before wasn't actually saved the script is running again ( like a new script ) .
what you need to do is creating a value in the model you have ( i am assuming its a model ) and manipulate the value like this : ( make sure its a BoolValue)
Plate = script.Parent.Parent.Plate Model = script.parent -- lets say the script is in a model isOn = script.parent.Power -- this value will keep changing by the function on() / off() function on() isOn.Value =true while true do wait(1) Plate.BrickColor = BrickColor.new("Persimmon") wait(1) Plate.BrickColor = BrickColor.new("Terra Cotta") end end function off() isOn.Value = false Plate.BrickColor = BrickColor.new("Medium stone grey") end function run() if isOn.Value == false then on() else off() end end script.Parent.ClickDetector.MouseClick:connect(run)