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