What is the best way to do something every x frames?

I’m making an object pool refilling system, and the best way I can think of making it work is by doing a check every x frames to see if the pool needs to be refilled. The only way I could think of doing this would be by using RenderStepped and setting up a currentFrame variable outside of the event listener’s scope, iterating it every frame, and then checking to see if currentFrame = x, and if so, setting x = 0 and doing the task. Is there a better way to do this?

What about a .Changed on the pool? If it changes, then check and refill, no need to constantly check with a loop every frame.

The pool is just a table of parts. It’s for a voxel game, and so when a chunk is loaded, potentially hundreds of parts could be taken out of it in a single frame. .Changed would work if the pool was a model, but then there’d be significantly more overhead when removing parts from the pool

You could probably implement a debounce type of system into the .Changed

The issue with .Changed here is that there is no model instance involved in this process for the reason mentioned above

If you wanna do the “every x frames” then you can do it like so:

local nFrames = 0
RS.RenderStepped:Connect(function()
    nFrames += 1
    if nFrames % x == 0 then
        --do the thing
    end
end)

The issue with is although yes, you are certainly checking it every x frames, constantly looping through possibly a very large table every “x frames” or so is probably a bad idea.