Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Does anyone know how to restart a while true do at the very top of the loop?

i need a code when a npc die it need to restart a loop

while true do
	--More Code that im not going to show cause it's just teleporting the player and stuff.

	game.Workspace.NPC:WaitForChild("humanoid").Died:Connect(function()
		for i = 1, #Players do
		if Players[i].Playing.Value == true then
			table.insert(Winners, Players[i].Name)
			Players[i].leaderstats.Money.Value = Players[i].leaderstats.Money.Value + 10
			Players[i].leaderstats.Wins.Value = Players[i].leaderstats.Wins.Value + 0.5
			Players[i].Character.Humanoid.Health = 0
			Players[i].Playing.Value = false
			ChangeText('GAME OVER!')
			end
		end
	end)
end

1 answer

✓ Accepted answer

By not doing what you're doing.

Why would you do this? Here's what it basically does:

It connects the died function. Once. Then twice. Then thrice. Then four times. Then eight times. Then 256 times. Then math.huge times. And now, when your humanoid dies, you all of a sudden have all players getting trillions of points and wins because you connect to the humanoid n amount of times per frame/ms.

You can't restart a while true do loop without using a function / creating an entire new loop. Rethink what you're doing and come up with a better solution.

Cheers.

2 comments

0

If you want, you can yield the loop until the humanoid dies and rerun your code, but I don't think that's what you want to do.

0

yes, you need to connect the Humanoid.Died somewhere out of the loop, and then turn the loop into a function which is called. Or use :Wait(), if that's what you need.

This discussion is preserved as it was. New answers, comments, and votes are closed.