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

While looping a for loop and pcall causes lag?

Hello so i've had this issue for a while now and i can't find any way to fix it. Heres the script:

LocalScript:

--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, "Transparency") end) --Im guessing pcall is the part that lags but im not sure

        if Transparency then
            Transparency = 1
        end
    end
end

The script works prefectly fine but it causes alot of lag. Try it out for yourself and if you find a fix that you would like to share, please tell me.

Any help is appreciated.

2 comments

0

You already asked this question, please don'

0

Why wont i get an answer? is it because no one has ever ran into an issue like this?

1 answer

While loop is a bad practice for running something repeatedly, it doesn't run based on the client FPS and it's very performance-consuming. To fix this lag, RunService.RenderStepped event will do the job. If you don't know what's RenderStepped, RenderStepped is fired every time a frame is rendered. So using RunService.RenderStepped is a good practice to run something repeatedly based on the client FPS. (Correct me if I'm wrong lol)

1 comment

0

Yeah, i've already tried renderstepped and it doesnt really fix it. In these kind of situations where the frame is dropping, RenderStepped just makes it worse because its repeating the loop every 1/60 seconds.

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