Last active
November 15, 2023 16:28
-
-
Save evaera/4ea10bf3cc8cb530b22e85485ab3f789 to your computer and use it in GitHub Desktop.
Roblox Lua throttle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttle(func, seconds) | |
local lastCalled = 0 | |
local callNumber = 0 | |
return function(...) | |
callNumber = callNumber + 1 | |
local currentCallNumber = callNumber | |
if tick() - lastCalled < seconds then | |
wait(seconds - (tick() - lastCalled)) | |
if callNumber ~= currentCallNumber then | |
return | |
end | |
end | |
lastCalled = tick() | |
return func(...) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment