How do I make it so the debounce applies to individual players instead of all of them?

So before I begin, I already know exploiters can insta-kill everyone with the script, but I added a votekick already so that’s dealt with (to an extent)

I know debounceTable exists but I’m not sure how to implement it.

Gun damage script:

local debounce = false
local fireRate = 1 -- (time between each shot)
local Players = game:GetService("Players")

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
	if debounce == false then
		local targetPlayer = Players:GetPlayerFromCharacter(target)
		if targetPlayer and targetPlayer.Team == player.Team then
			return
		end
		debounce = true
		if target:FindFirstChild("Humanoid") then
			target.Humanoid:TakeDamage(30)
		end
		task.wait(fireRate)
		debounce = false
	end
end) 

Gun sound script:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local sound = handle:WaitForChild("FireSound")

local debounce = false

tool.Activated:Connect(function()
	if not debounce then
		debounce = true
		sound:Play()
		wait(1)
		debounce = false
	end
end)

And finally, the local script if you need it:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	local target = mouse.Target
	if target.Parent:FindFirstChild("Humanoid") then
		script.Parent.RemoteEvent:FireServer(target.Parent)
	end
end)

If you know how to update this to making debounce for each player instead of all of them, thanks!

Also I really don’t need a script that makes me change the entire system because of exploiters, no matter what script you use, there will always be exploiters, thanks again.

You can use a table and insert and remove the corresponding player, so instead of

debounce = false

You’d have

debounce = {}

Example for the damage script:

local debounce = {}
local fireRate = 1 -- (time between each shot)
local Players = game:GetService("Players")

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
	if not table.find(debounce,Player) then
		local targetPlayer = Players:GetPlayerFromCharacter(target)
		if targetPlayer and targetPlayer.Team == player.Team then
			return
		end
		table.insert(debounce,Player)
		if target:FindFirstChild("Humanoid") then
			target.Humanoid:TakeDamage(30)
		end
		task.wait(fireRate)
		table.remove(debounce,table.find(debounce,Player))
	end
end) 

I’d rather use a dictionary than an array for this, although both methods are perfectly valid.

3 Likes

Ok thanks, I’ll try that later because my laptop is dead :confused:

Also do I have to change the sound or local scripts?

The damage part of the script works, but the debounce is now broken.

If you copy-pasted Kira’s code verbatim, note that it has a problem with Player (uppercase P) being used instead of player, which will make it not work as-is.

Consider a dictionary map for simplicity:

local debounce = {}
local fireRate = 1 -- (time between each shot)
local Players = game:GetService("Players")

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
	if not debounce[player] then
		local targetPlayer = Players:GetPlayerFromCharacter(target)
		if targetPlayer and targetPlayer.Team == player.Team then
			return
		end
		debounce[player] = true
		if target:FindFirstChild("Humanoid") then
			target.Humanoid:TakeDamage(30)
		end
		task.wait(fireRate)
		debounce[player] = nil
	end
end) 

FWIW, this is not really a “debounce” as it’s not meant to filter unintentional duplicate input. The normal game terminology term for this mechanism is a “cooldown” timer.

1 Like

I fixed the debounce issue, thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.