Why is this spawn function crashing my game?

Hey guys! Would somebody be so kind to tell me why the following spawn() function in a LocalScript is crashing my game client.

Please Note: There are no errors that are raised and this code works perfectly other than the crashing of the game.

spawn(function()
	wait(1)
	while true do
		if not Cooldown then
			Cooldown = false
		end
	end
end)

Many thanks,

Gandalf

while is always ‘true’ which causes it to crash since it is infinitely looping. You will need a condition that will make the while loop false at a certain point so it will stop infinitely looping.

Maybe you mean something like:

Cooldown = true
spawn(function()
	wait(1)
	while Cooldown == true do
		if Cooldown then
			Cooldown = false
		end
	end
end)
2 Likes

Infinite loops are fine, the issue is that the infinite loop does not a contain a yield, which results in the script exhausting its resources.

spawn(function()
	wait(1)
	while true do
		wait(1)
		if not Cooldown then
			Cooldown = false
		end
	end
end)

The “wait(1)” I’ve added into the while loop will cause the thread to yield preventing the script from crashing.