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

Hey, im working on a game and i fell over this problem. Easy fix?

Asked by 5 years ago
Edited 5 years ago

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.

0
In the past I was told that Roblox has an issue with ending scripts by disable and an issue with cutting off threads. Also, in filteringenabled you'll notice issues of objects being removed, such as scripts, but never actually leaving from the server requiring extra checks. alphawolvess 1784 — 5y
0
Dont use while true do, use script.Parent.Changed:Connect(), having a lot of these scripts with whilet rue do can cause lag. LoganboyInCO 150 — 5y

1 answer

Log in to vote
0
Answered by
joeldes 201 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
Hey, im very grateful for your answer. I've tried a while now to google toggleable scrips, but without any luck. I simply can't find the right place to learn about them. Do you know any website that can teach me how to make a toggleable script? And again, thank you for your answer. Very grateful :) Victorhjelmberg 11 — 5y
0
According to Lua documentation, it's invalid to have a "return" or "break" that is not the last statement of a block, but you can get around this by using a "do ... end" block. So "print(1); print(2); print(3); do return nil end; print(4);" would be a valid program where the "print(4);" never happens. aschepler 135 — 5y
0
You could always have a loop that has an IF stament inside of it that checks if a variable is true or false. Then if the var is true you would run the code @Victorhjelmberg joeldes 201 — 5y
Ad

Answer this question