Okay, so im curently making a game in roblox and i found this problem.
I though that it was weird that my scripts would keep on going even though i had made them disable themself, so i tried to test it with this little code
while true do if script.Disabled == true then print ("Im Dead") else print ("Im Alive") end wait(0.2) if script.Disabled == false then script.Disabled = true print ("Disabled") end if script.Disabled == true then print ("Im Dead") else print ("Im Alive") end end
It could probably have been made easier, but i though this was fine. What i wanted to see with this code was if the script would disable itself, and then stop afterwards, but when i looked at the output i was amazed. It looked like this:
Im Alive Disabled Im Dead Im Dead Im Dead Im Dead
and it just kept going on saying "Im Dead". I just found this a bit weird. I didn't understand why the script kept going on even though it said it was disabled. I tried putting the killswitch on another part with this script.
function onTouched() workspace.Script.Disabled = true end script.Parent.Touched:connect (onTouched)
But the output was still the same. Even though i touched the killswitch. After i tried to remove the
if script.Disabled == false then script.Disabled = true print ("Disabled") end
part in the (while true do) script. And when i tested it, i was amazed. The output now looked like
Im Alive Im Alive Im Alive Im Alive Im Alive
ect. ect. And when i hit the killswitch the output just stoped, immediately, like it should if the while true script disabled itself
I don't know why this happens. Some of you must have run into this problem before. Can you help me? I have ideas to fix this problem, but they are not optimal at all, so i hoped you could help me.
Very interesting. While I'm not sure why the scripts keep running after they are disabled, it shouldn't matter.
Disabling a script is not a good way to stop a loop or any block of code from running.
If you are trying to "kill" a loop after a certain condition is met, you should always use break
to do this.
Here is an example:
print("1") print("2") print("3") return nil print("4")
Attempting to run this code will result in an output of 1, 2, 3
The last print would be underlined red and display a syntax error screaming excepted <eof>, got print()
EOF stands for End Of File
When you need a script to stop you must always use return
or return nil
to stop the script from running. Any other method aside is not safe and could end with unexpected results.
If you are trying to make a loop toggleable, there are better ways of doing so. I will not be going over that here, but a little google on that may get you going in the right direction.