So basically, I have a tag system (like the ones the linked sword model has), the issue is that it waits like 3 seconds before untagging the player, so if I spam attack a player, the wait’s will stack up then bug the system out. Any tips here to prevent this annoying bug?
If it’s fired again and again and again, it will fire the same function over and over, and reset the tag over and over, not optimal when I’m working with something that says “You cannot leave combat!”, the text will appear and disappear very fast, that’s the issue.
I can’t set a delay either, because other players should be able to tag them too.
local debounce = {};
if not debounce[player] then
-- do
debounce[player] = true
-- whatever code
wait(3)
debounce[player] = false -- turns off cooldown
else
-- on cooldown!
end
The issue with that is that other players can tag them too, which means the script will run multiple times.
Then we have the same issue again, this only work if there’s only 1 player hitting them before the cooldown expires.
Okay then. I’ll just focus on preventing the bug, since I have no idea how the tagging thing will work:
local debounce = true
local taggers = {}
if not table.find(taggers, tagger) then
table.insert(taggers, tagger)
if debounce then
debounce = false
--do code here
task.wait(3)
taggers = {}
debounce = true
end
end
You could try storing the timestamp of when they were last tagged and if it is old enough, let them leave combat. Or move the tag tracking into a single script so that you can properly terminate threads when they are no longer needed.
Of course, it would help if you told us what you needed this for. It seems like you just need it for checking if they are in combat, in which case you could just use the timestamp approach.
The tag is created when the player joins, it’s never deleted, and I only run into the same issue when removing it again if I’m gonna do what you suggest.