Help limiting how many times a second a player can do something

I am working on a game where you kill npc’s using swords, However a exploiter can fire the attack event 1000 times in a second and instant kill a npc, I am wanting to limit how many times they can send the event and if they send it for instance 20 times in a second then it will auto kick them. OR if you have any other better way of detecting this, That would help as well.

Make a InUse = {} table on attack add them to the list and before the event is fired check if there already in the list if they are return else do attack at the end of the attack remove them from the table

There are a-lot better ways of doing this but I like this method, fast,simple,clean

Sure there is a better way, But this did the job. Thanks!

: D
Nice, if needed i could leave you sample code?

No need, Here is what i did.

local WAIT = 0.1
local Cooldown = {}
local plr = plrthatisattacking
if table.find(Cooldown, plr.Name) then
	plr:Kick("Attacking to fast.")
	return
else
	table.insert(Cooldown, plr.Name)
	coroutine.wrap(function()
		wait(WAIT)
		table.remove(Cooldown, Cooldown[plr.Name])
	end)()
end
2 Likes

This is good, make sure you add a client cooldown, just so normal players dont get kicked if they spam to much.

I did, It waits for the animation to be completed then allows them to attack again.Which the animation is way longer than 0.1 so shouldn’t have any issues.

1 Like

:+1:
Nice, hope it helped!

#BlockedBy30CharNeeded

1 Like

Instead, use benchmarking. This is just my personal advice and the method BugleBoy provided is sufficient enough.

WAIT = 0.1
cooldowns = {}

------------------------


if (cooldowns[plr] and os.clock() - cooldowns[plr] < WAIT) then
	return
end
cooldowns[plr] = os.clock()
-- do action
2 Likes

Hmm i see why this is better, Even tho they are the same thing, Ill use your method instead.

1 Like

This works, very well.
I have done something like this before.

1 Like