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

While looping a for loop causes lag?

Asked by 3 years ago

Hello so i've ran into an issue. Whenever i loop a for loop, it creates enormous lag and delay. Is there any fix to this?

LocalScript:

--Example script, there might be errors since i just wrote this code.

    while wait() do
        for i,v in next, game:GetDescendants() do
            if v:IsA("Instance") then
                v.Changed:Connect(function(p)
                    game:GetService("ReplicatedStorage"):FindFirstChild("Prop", true):FireServer(v, tostring(p), v[tostring(p)])
                end)
            end
        end
    end


Simple. Incase if you dont understand, the localscript is supposed to get every object in game and detect if the property change.

ServerScript (Prop remote):

local PropRemote = game:GetService("ReplicatedStorage"):FindFirstChild("Prop") or false


PropRemote.OnServerEvent:Connect(function(plr, part, prop, value)
    part[prop] = value
end)

NOTE: Yes i do know that i can do it from a serverscript without needing remote events.

0
Pretty sure the while loop, will feel like it's not looping every 5 seconds instead of 6 I'd avoid using wait(), it's not accurate. but; "while wait(5) do"; should stop the lag for now MarkedTomato 810 — 3y
0
Use ipairs instead of next() MarkedTomato 810 — 3y
0
Didnt really fix it RemsFriend -24 — 3y

Answer this question