Preventing spam for task.wait()

Hi!

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.

Debounce!

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.

Not if you put the debounce[player] with player being the person who already tagged them

Then another player can tag them then.

Check for both…

if not debounce[player] and not debounce[tagger] then
-- player = person being tagged
-- tagger being person tagging player
end

I’ve found a possible solution.
Here’s an example of what I mean:

local tagger

if not tagger then
	task.wait(3)
	--do what you want
	tagger = nil
end

This will only work if tagger does not exist, meaning that only one player will be able to tag them.

The player being damaged will have the tag.
So that wouldn’t make sense.

meaning that only one player will be able to tag them.

That’s what I didn’t want. But I have come to fear that this might be the only solution, even before I posted.

Oh. Then, you might be able to use table.find() with a table of tagger players stored:

local taggers = {}

if not table.find(taggers, tagger) then
	table.insert(taggers, tagger)
	--do code here
	task.wait(3)
	taggers = {}
end

That also means that they cannot tag them again before it expires, which is still preventing them from tagging.

In cases where there are 2 players chasing them, this wouldn’t work.

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

That doesn’t work either.
So I have no idea.

Well, your task.wait() spam should be fixed with it.

Not sure about your tagging system, though.

Yeah but still, only 1 can tag the player at a time, which is another issue.

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.

I don’t even know what you’re trying to achieve at this point

You can look for the tag, if it exists than change its value if not than create a new one.

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.

1 Like