While loop stops or doesnt after script destroyed?

I have a while true do loop in a script, and it keeps running even after I do script:Destroy(). This has never before happened to me, so I decided to do further testing.

I created another script with while true do loop (and print inside it), and tried to destroy it in different ways. Destroyed it from the same script, from another script, from studio with backspace. In all cases the while loop stopped running.

The only difference between those 2 scripts, is that the first one, which doesnt stop the loop on destroy, has bunch of other things in it besides the loop.

So what I want to know:
Why does sometimes while loop stop when script is destroyed, and sometimes it doesnt. What influences this?

More details if anyone needs it:
Script who’s loop doesnt stop no matter how I destroyed it:
I tried destroying this from within itself as you can see here, tried destroying it from another script. In both cases while loop still runs.

local gameInfo = game.ReplicatedStorage:WaitForChild("GameInfo")
local bluePoints = gameInfo:WaitForChild("BluePoints")
local redPoints = gameInfo:WaitForChild("RedPoints")

local oneTimeEvent = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("OneTimeEvent")


local flagPoses = workspace.Map:WaitForChild("CurMap"):WaitForChild("FlagPoses")
local blueFlag = flagPoses:WaitForChild("BlueFlag")
local redFlag = flagPoses:WaitForChild("RedFlag")

local flagTemplate = game.ReplicatedStorage:WaitForChild("ToBeCloned"):WaitForChild("TakeFlag")



local blueTaken = false
local redTaken = false
local blueTakerConn = nil

local redTakerConn = nil



local timer = gameInfo:WaitForChild("Timer")
timer.Value = 4*60

while true do
	task.wait(1)
	print(timer.Value)
	print(script.Parent)
	if timer.Value > 0 then
		timer.Value -= 1
	end
	
	if timer.Value <= 0 then
		
		if redPoints.Value > bluePoints.Value then
			oneTimeEvent:FireAllClients(3, "Red team wins. All the team members rewarded with gold.")
		elseif bluePoints.Value > redPoints.Value then
			oneTimeEvent:FireAllClients(3, "Blue team wins. All the team members rewarded with gold.")
		else
			oneTimeEvent:FireAllClients(3, "Game ends with a draw.")
		end
		
		
		
		script:Destroy()
	end
end

Script who’s while loop stops after destroying it, no matter how I destroy it:

while true do
    task.wait(1)
    print(script.Parent)
end

Maybe a sneaky lua coroutine like spawn or wait. I could see task.wait being destroyed but wait remaining after. I don’t believe destroying a script has ever been a guaranteed way to stop/cancel it, best to stop the code with code rather than removing it.

Both use only task.wait(), none uses wait or any other coroutines. I posted both scripts above.

That will be a major issue, as there are 100+ scripts in the game, some of which again using while loop.

My main problem is this has never happened before, always destroying stopped the script, disconnected connections inside it and broke the loops, as intended.
So I’m doing something wrong this time, question is what.

you can re-write your script to not loop forever. I would recommend doing this for all of your other 100 scripts and going forward.

while timer.Value > 0 do
	task.wait(1)
	print(timer.Value)
	print(script.Parent)
	timer.Value -= 1
end


if redPoints.Value > bluePoints.Value then
	oneTimeEvent:FireAllClients(3, "Red team wins. All the team members rewarded with gold.")
elseif bluePoints.Value > redPoints.Value then
	oneTimeEvent:FireAllClients(3, "Blue team wins. All the team members rewarded with gold.")
else
	oneTimeEvent:FireAllClients(3, "Game ends with a draw.")
end

script:Destroy()

maybe change this to
while script and script.Parent do
to make it stop when the script is destoryed

1 Like