Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
AronYstad 101
3 years ago

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

Log in to vote
0
Answered by 3 years ago

Try using:

while wait(0.01) do

Hope this helps!

Ad

Answer this question