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

While looping a for loop and pcall causes lag?

Asked by 3 years ago

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.

0
You already asked this question, please don' DietCokeTastesGood 111 — 3y
0
Why wont i get an answer? is it because no one has ever ran into an issue like this? RemsFriend -24 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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)

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. RemsFriend -24 — 3y
Ad

Answer this question