Making a debounce for your connections can be a pain so I made a quick and easy module for that! It is also performant since it doesn’t do any yielding but uses tick() instead, which also makes it SUPER accurate aswell! It will also pass all arguments aswell!
It will only call a callback function once enough time has passed from the last fired event. And you can use a TimeDelay argument to set it’s delay between those event fires. It returns a TimedConnection which has a method called :ChangeDelay() which will change the TimeDelay variable.
Get it here:
https://www.roblox.com/library/7458984899/TimedConnection
Or copy the Source Code:
local TimedConnection = {}
TimedConnection.__index = TimedConnection -- OOP
--// Type Checking
type callback = {}
type void = {}
type TimedConnection = {TimeDelay: number, Connection: RBXScriptConnection}
--// Main Methods
TimedConnection.new = function (Event: RBXScriptSignal, callback: callback, TimeDelayArg: number) : TimedConnection
local self : TimedConnection = {}; setmetatable(self, TimedConnection)
local CurrentTime = tick() - 1
self.TimeDelay = TimeDelayArg
self.Connection = Event:Connect(function(...)
if tick() > CurrentTime then
callback(...)
CurrentTime = tick() + self.TimeDelay
end
end)
return self
end
function TimedConnection:ChangeDelay (Value: number)
self.TimeDelay = Value
end
return TimedConnection
I also made a test script to show what it can do!
Code:
local TimedConnection = require(script.Parent.TimedConnection) -- Change the path if you want to run this code
local RunService = game:GetService("RunService")
local Connection = TimedConnection.new(RunService.Stepped, function(delta)
print(delta)
end, 1) -- Delay is 1 seconds by default
task.wait(3); Connection:ChangeDelay(0.1) -- Will wait 5 seconds then change the delay to 0.1
Video:
Hopefully you guys found this useful! Reply with something you would want me to add!