A loop stops if I disable the script?

If I have a script in ServerScriptService which is disabled.
Then I turn it on while “the game is running” for players in server
That script is a while loop:

while true do
  print("yup")
  wait(1)
end

If I disable that script X seconds later, the loop actually stops or will continue?

I have tested this and the loop continues. However, in the documentation it states that the loop will stop. Very strange :man_shrugging:

1 Like

May I ask why you would want to disable and re-enable the script? Typically that is not a best practice and it would be better to just break the loop using break.

1 Like

If I run this test, what could happen with the loops?

A server script in ServerScriptService, its enabled since the start:

wait(10)
warn("start")
-- ENABLE THE SCRIPT THAT WILL FIRE THE LOOPS
script.Parent:WaitForChild("loop").Disabled = false

wait(10)

warn("stop")
-- DISABLE THE SCRIPT THAT WILL FIRE THE LOOPS
script.Parent:WaitForChild("loop").Disabled = true

A server script in ServerScriptService, its disabled, it will be enabled by the previous script after 10 secs

local ModuleScript = require(script.Parent:WaitForChild("ModuleScript"))

task.spawn(function()
	ModuleScript.AnotherLoop()	
end)

task.spawn(function()
	ModuleScript.AnotherSpawnedLoop()	
end)

task.spawn(function()
	while true do
		warn("spawn while loop in script")
		wait(0.5)
	end
end)

task.spawn(function()
	while true do
		print("simple while loop in script")
		wait(1)
	end	
end)

A module called ModuleScript in ServerScriptService to run 2 more loops Called by script that was disabled:

local ModuleScript = {}

ModuleScript.AnotherLoop = function()
	while true do
		print("module while loop called by script")
		wait(0.2)
	end
end

ModuleScript.AnotherSpawnedLoop = function()
	task.spawn(function()
		while true do
			warn("module while loop called by script with spawn")
			wait(0.2)
		end	
	end)
end

return ModuleScript

Any loop will be disabled or will continue?

Its just a question 30k of chaaaaaaars