I have a tool in my game that basically scans things using Instance.new(‘ProximityPrompt’). After the newly created ProximityPrompt gets Triggered the player gets rewarded and the Proximity Prompt gets deleted.
My question is what are the best ways to add a debounce to the scanner that just scanned something?
I would just store the time of the last action of each player (I prefer os.clock because of its accuracy) and then just simply compare that to the current time and go on from there
something like this
--we will be using attributes for this example, but you should use a regular Lua table if there are a lot of debounces
--if you do end up using tables, make sure there's a proper garbagecollection system to go along with it
local pl = game:GetService'Players'
local function init(p: Player) --this function will initialize the player with a time of zero
p:SetAttribute('debounce', 0)
end
local function checkDebounce(p: Player, d: number): boolean --returns true if the debounce cooldown has passed
local now = os.clock() --get current time
if now - p:GetAttribute'debounce' >= d then --check if the time difference is at least the set debounce time
p:SetAttribute('debounce', now) -- re-set the time of last usage
return true
end
return false
end
pl.PlayerAdded:Connect(init) --automatically initialize new players who join the game
for _, v: Player in pl:GetPlayers() do --initialize players that are *already* in the game
init(v)
end
In my game you scan multiple things, what I want to achieve is when one Thing is scanned other things would still be scannable, I think putting the debounce in the player would not let me achieve this. Or if there is that’s not expensive, how?
Anyways for the server script to remember that scanned thing and detect when the countdown of that thing has already passed?
I also need the other players to get affected by the debounce