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

For loop not working if while loop present?

Hello, I'm practicing with for loops and the script work until the while loop is in there. Any answers of what I did wrong?

local part = script.Parent
local db = true


part.Touched:Connect(function(player)
	local char = player.Parent
	local humanoid = char:FindFirstChild("Humanoid")
	if humanoid and db then
		db = false
		for i, v in pairs(char:GetChildren()) do
			if v:IsA("MeshPart") then
				local fire = Instance.new("Fire")
				fire.Parent = v
				
				while  do
					humanoid:TakeDamage(10)
				end
			db = true	
			end
		end
	end
end)

1 answer

You need an argument in between the while and do. So, with while loops, here's how they work:

While the statement in between the while and do is true, the code inside it will run. If you want a loop that lasts forever, do: while true do, because true is always true. Just remember, when using while true do, use a wait() at the end of your code or else you will get the following error: Script allowed exhaustion time, and it will also cause lag for the client.

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