Damaging a player once with a tool

So, I’m trying to make it so that if the player activates a tool it waits half a second before damaging the other player.

So, I haven’t found a way to make it wait half a sec before damaging, and the problem is if the tool is activated it will still be able to damage the other player after like 3 secs

Its a Local Script in the tool.

local animationmodule = require(game:GetService("ReplicatedStorage").modules.things)
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local handle = tool:WaitForChild('Handle')
local cd = false
local animcd = false
local waittime = 3.25
local humanoidsTouched = {}
local hitbox = tool:WaitForChild("hitbox")

tool.Activated:Connect(function()

	if cd == false then 
		cd = true
		animationmodule.PlayAnimId(plr, 10959744156)
		wait(3.25)
		cd = false
       end
end)

tool:WaitForChild("hitbox").Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and cd == true then
		if  table.find(humanoidsTouched, humanoid) == nil then
			table.insert(humanoidsTouched, humanoid)
			humanoid.Health -= 30
			task.wait(waittime)
			table.remove(humanoidsTouched, table.find(humanoidsTouched, humanoid))
		end
	end
end)

You want to make a debounce for the Touched function:

local animationmodule = require(game:GetService("ReplicatedStorage").modules.things)
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local handle = tool:WaitForChild('Handle')
local cd = false
local animcd = false
local waittime = 3.25
local humanoidsTouched = {}
local hitbox = tool:WaitForChild("hitbox")

local ToolHitDebounce = false;

tool.Activated:Connect(function()

	if cd == false then 
		cd = true
		animationmodule.PlayAnimId(plr, 10959744156)
		wait(3.25)
		cd = false
       end
end)

tool:WaitForChild("hitbox").Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and cd == true and not ToolHitDebounce then
        ToolHitDebounce = true;
		humanoid.Health -= 30
		task.wait(waittime)
		ToolHitDebounce = false;
	end
end)

You could do this,

local animationmodule = require(game:GetService("ReplicatedStorage").modules.things)
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local handle = tool:WaitForChild('Handle')
local cd = false
local animcd = false
local waittime = 3.25
local hitbox = tool:WaitForChild("hitbox")

tool.Activated:Connect(function()

	if cd == false and hitbox then 
		cd = true
		
		local c

		c = hitbox.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
			if humanoid and cd == true then
				humanoid.Health -= 30
				c:Disconnect()
			end
		end)
		
		animationmodule.PlayAnimId(plr, 10959744156)
		task.wait(waittime)
		cd = false
	end
end)

This would only damage one player and the .Touched would not run until the tool is activated again. Is this what you’re looking for?

Adding up to the other answers, do not damage players on the client, it would be wise for the server to know the player’s health.

Edit: I’m pretty sure touched events don’t fire on the client.

2 Likes

I didn’t even realise he said it’s in a Local Script, nice catch!

2 Likes

There’s actually a module that you can use that may solve your problems.

It uses attachments and raycasting, so it’s very optimal to use, and pretty accurate.

I’ve tried raycasting and it works pretty well, but i cant figure out how to blacklist the player whos wholding the tool. If you are able to, help is appreciated.
Here is the script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)
local tool = script.Parent.Parent
local cd = false
 game:GetService("Players").PlayerAdded(function(plr)
local MyCharacter = plr.Character or plr.CharacterAdded:Wait()



local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {MyCharacter}
Params.FilterType = Enum.RaycastFilterType.Blacklist
local newHitbox = RaycastHitbox.new(script.Parent)
newHitbox.RaycastParams = Params
newHitbox.Visualizer = true
newHitbox.DebugLog = true




newHitbox.OnHit:Connect(function(hit, humanoid)
	print(hit)
	humanoid:TakeDamage(30)
end)

tool.Activated:Connect(function()
	if cd == false then
		cd = true
		newHitbox:HitStart()
		wait(1)
		newHitbox:HitStop()
		wait(2.25)
		cd = false
		end
	end)
end)

Your code is broken, you forgot to connect the function on line 5.

Here is a fixed code using a local function and character:GetChildren() instead of character.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)
local tool = script.Parent.Parent
local cd = false

local function onPlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()

	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {character:GetChildren()}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local newHitbox = RaycastHitbox.new(script.Parent)
	newHitbox.RaycastParams = Params
	newHitbox.Visualizer = true
	newHitbox.DebugLog = true




	newHitbox.OnHit:Connect(function(hit, humanoid)
		print(hit)
		humanoid:TakeDamage(30)
	end)

	tool.Activated:Connect(function()
		if cd == false then
			cd = true
			newHitbox:HitStart()
			wait(1)
			newHitbox:HitStop()
			wait(2.25)
			cd = false
		end
	end)
end

game:GetService("Players").PlayerAdded:Connect(onPlayerAdded)

I don’t know how your ModuleScript looks like, if there’s any errors in there, I can’t fix them right now.

Look at the documentation linked onto the original post about the module, it will show you a way to blacklist yourself.