Curious about the Script Engine

Hello Roblox Community,
As the title says, I have a question about Script Engines.
Let me give you an example.

print("First Print")
task.wait(10)
print("Second Print")

If I typed this code in the Script and run, it will print “First Print” and after 10 seconds, it will print “Second Print” in the Output.

But If I run the Script and I disable the Script before “Second Print” print out and I enable the Script again, it prints “Second Print” immediately even though the code didn’t wait 10 seconds.

Could you please explain why this is happening or if the engine is like this, please send me the document or topic referenced? Thank you.

1 Like

In the clip, I disable and re-enable the Script after the first Hello, World!, before the second one, causing it to reset and start from the beginning. It waits the full 10 seconds before printing the second one instead of printing instantly like you describe.

1 Like

That’s… not how it works.

I just tested this too and it will restart the code each time it is disabled and re-enabled. @Judgy_Oreo 's video also demonstrates this.

When a script is disabled, the execution of the Luau code within it is halted. When the script is re-enabled again, the code will start executing again from the beginning. This is from the script’s behaviour, where when it’s created and the source has loaded, the script is enabled and starts running due to being enabled. The same applies here when you enable it.

3 Likes

Thank you, in the actual game, there was a misunderstanding by putting the same argument in print(). So, is there a way to pause the code and have it continue running?

You could use coroutines, or a repeat until block.

local func = coroutine.create(function()
    print("one")
    coroutine.yield()
    print("two")
end)

coroutine.resume(func)
task.wait(10)
coroutine.resume(func)
print("one")

repeat
    task.wait()
until
    condition

print("two")
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.