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

While looping a for i,v loop lags game?

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.

1 comment

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.

1 answer

✓ Accepted answer
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!

2 comments

0

Thanks man, i really didnt know that a single wait(.001) could fix it. You helped me alot.

0

No problem

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