How To: Code a Throttle or Debounce function in Lua

Okay, so I've been dabbling with some Lua game programming in Roblox. No jokes, it's kind of fun and my kids think I'm cool because I can build games for them.

No beating around the bush. Here’s a module that you can drop into your Roblox Place and start using it right away to debounce function calls.

local Throttler = {}

local running = false

function callback, timeoutInSec )
	if running == false then

		running = true


		if timeoutInSec ~= nil then

		running = false

return Throttler

Use the Lua module like so:

local ServerStorage = game:GetService("ServerStorage")
local Modules = ServerStorage:WaitForChild("Modules")

local throttler = require(Modules:WaitForChild("Throttler"))

function myOtherFunction()
end myOtherFunction, 0.5 )

My example loads the debounce function from ServerStorage, but you could place it in ReplicatedStorage if you need to share it between client/server.

This will ensure your function cannot be executed multiple times in parallel. You could use this in cases where a player touches a portal or object that causes damage so the logic doesn’t execute too many times.