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

While looping a for i,v loop lags game?

Asked by 3 years ago

How can i fix the issue(read title)

Heres an example script of what i mean.

--Example

local function getProp(obj, prop)
    local getPropVar = obj[prop]
    return getPropVar
end
local protectedCall = pcall

while wait() do
    for i,v in next, game:GetDescendants() do
        local Transparency = protectedCall(function() getProp(v, "Tranparency") end)

        if Transparency then
            Tranparency = 1
        end
    end
end

Running this lags you.

Is there any way to reduce the lag or get rid of the lag?

Any help is appreciated.

0
Hey, just an fyi. Im pretty sure you dont even have to put a value into the wait() you can just leave it as "wait()" and it shouldnt lag. I might be wrong though. tightanfall 110 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
local function getProp(obj, prop)
    local getPropVar = obj[prop]
    return getPropVar
end
local protectedCall = pcall

while wait() do
    wait(0.001) -- Example
    for i,v in next, game:GetDescendants() do
    wait(0.01) -- Example
        local Transparency = protectedCall(function() getProp(v, "Tranparency") end)

        if Transparency then
            Tranparency = 1
        end
    end
end

So the issue with your "for loop" is that it is running without any restriction so you just need to add a wait(). It can be as small as wait(0.001) but if your script has no restriction it will just use as many resources as it can to go as fast as it can.

Hope I helped!

0
Thanks man, i really didnt know that a single wait(.001) could fix it. You helped me alot. RemsFriend -24 — 3y
0
No problem tightanfall 110 — 3y
Ad

Answer this question