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

Why does my while true loop stop after a few iterations?

So I made a while true loop to make fire spread in my game. The first iteration it spreads fine, the second iteration it only spreads to a few of the bricks, and after that, the code seems to completely stop. Also, the game lags quite a lot when the loop runs, so any ideas on how to optimise it? Here is the code:

while true do
	for i,part in pairs(game.Workspace.Map:GetDescendants()) do
		local Checked = part:FindFirstChild("Checked") or Instance.new("BoolValue",part)
		Checked.Name = "Checked"
		Checked.Value = false
	end
	wait(5)
	for i,part in pairs(game.Workspace.Map:GetDescendants()) do
		if part:IsA("BasePart") then
			if part:FindFirstChild("OnFire") then
				if part:FindFirstChild("FireTime") then
					if part.OnFire.Value == false then end
					if part.OnFire.Value == true then
						part.Parent = game.Workspace.Immune
						if part:FindFirstChild("Checked").Value == false then
							local minvector = part.Position-(part.Size/2)-Vector3.new(1,1,1)
							print(minvector)
							local maxvector = part.Position+(part.Size/2)+Vector3.new(1,1+part.FireTime.Value,1)
							print(maxvector)
							part.FireTime.Value += 1
							local Region = Region3.new(minvector,maxvector)
							for _,spreadpart in pairs(game.Workspace:FindPartsInRegion3(Region,game.Workspace.Immune,math.huge)) do
								if spreadpart:FindFirstChild("OnFire") and spreadpart.Name ~= "Smoke" and spreadpart:IsA("BasePart") then
									spreadpart.OnFire.Value = true
									spreadpart.Checked.Value = true
								end
							end
						end
					end
				else
					local firetime = Instance.new("IntValue")
					firetime.Parent = part
					firetime.Name = "FireTime"
				end
			else
				local OnFire = Instance.new("BoolValue")
				OnFire.Parent = part
				OnFire.Value = false
				OnFire.Name = "OnFire"
			end
		end
	end
end

1 answer

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