Exploit Prevention (Version 1)

Don’t use delay() or wait() in your code, it’s considered code smell / bad practice. Instead, you can do a throttled RunService.Stepped loop:

local Step = game:GetService("RunService").Stepped

local function throttle(n, func)
    local iterations = 0
    return function(...)
        iterations += 1

        if iterations >= n then
            iterations = 0
            func(...)
        end
    end
end

local connection
connection = Step:Connect(throttle(20, function(total, delta)
    -- once player leaves, make sure to do connection:Disconnect()
end))

This way, you also have access to the time between each frames. (I chose Stepped because it fires before the physics happens each frame. However, you can replace it with Heartbeat if you want.)

2 Likes