Sword Does Damage More Than Once

Whenever I do an attack with my sword, even if I do not attack again it’ll still do damage if it clips through a player, is there any way to make it do damage only one time? My code consists of a local script that just fires a remote event and a script that looks for the remote event and then does damage to the thing it hits, but it keeps doing damage after the attack has ended.

I’ve tried to make a bool that sets a variable to true so that it only does damage once, but then I cannot find a way to turn that variable back to false in order to deal damage again.

Gif: https://gyazo.com/c88b633910fe3b038950e4d496cdfa61

Code in script

local tool = script.Parent

tool.LeftSwing.OnServerEvent:Connect(function()
	tool.Hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(25)	
		end
	end)
end)

tool.RightSwing.OnServerEvent:Connect(function()
	tool.Hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(25)	
		end
	end)
end)
1 Like

Add a debounce thing inside your script.

1 Like

I already tried but there is no way to turn it false afterwards.

local tool = script.Parent
local damageDebounce = false

function Damage(hit, connection)
    if hit.Parent:FindFirstChild("Humanoid") then
        if not damageDebounce then
            damageDebounce = true
		    hit.Parent.Humanoid:TakeDamage(25)	
            pcall(function()
                   connection:Disconnect()
                   connection = nil
            end)
            delay(2.5, function()
                damageDebounce = false
            end)
	    end
    end
end

tool.LeftSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
	end)
end)

tool.RightSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
	end)
end)

This happens because the Touched event fires whenever the object is touching a part. If you only want the player to do damage after they stop hitting the other player, you can use TouchEnded (not the best event) to set the debounce to false which signals that they stopped touching the player.

This code doesn’t work, it just delays the damage from happening https://gyazo.com/fe90e1df1f37f22f3651b0113672bd48

Yeah that is what it is supposed to do.

If it only delays the hit then it can still happen two times which is what I’m trying to avoid

I also changed it so it disconnects the Touched event

local tool = script.Parent
local damageDebounce = false

function Damage(hit, connection)
    if hit.Parent:FindFirstChild("Humanoid") then
        if not damageDebounce then
            damageDebounce = true
		    hit.Parent.Humanoid:TakeDamage(25)	
            pcall(function()
                   connection:Disconnect()
                   connection = nil
            end)
            delay(2.5, function()
                damageDebounce = false
            end)
	    end
    end
end

tool.LeftSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
	end)
end)

tool.RightSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
	end)
end)

The problem is because you dont disconnect the event, try disconnecting it.

In case you dont know how to disconnect…

local connection
connection = mouse.Button1Down:Connect(function()
       connection:Disconnect()
end)
-- OR
local connection
connection = mouse.Button1Down:Connect(function()
      
end)
connection:Disconnect() -- This would disconnect right after, just placed this so you know that you can disconnect outside the event too

Used button1down as a example but you can use anything else

1 Like

Disconnect the .Touched event after you damage the player.

Do you mean disconnecting the function or disconnecting the button press? I didn’t know you can do that

Disconnecting the Button1Down/Button1Up will still damage the player/npc because the touched event is being used on the server.
Use this code instead:

local tool = script.Parent
local damageDebounce = false

function Damage(hit, connection)
    if hit.Parent:FindFirstChild("Humanoid") then
        if not damageDebounce then
            damageDebounce = true
		    hit.Parent.Humanoid:TakeDamage(25)	
            pcall(function()
                   connection:Disconnect()
                   connection = nil
            end)
            delay(2.5, function()
                damageDebounce = false
            end)
	    end
    end
end

tool.LeftSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
	end)
end)

tool.RightSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
	end)
end)

That code still damages the npc after I swing.

Let me try something else then.

Try this:

local tool = script.Parent
local damageDebounce = false

function Damage(hit, connection)
    if hit.Parent:FindFirstChild("Humanoid") then
        if not damageDebounce then
            damageDebounce = true
		    hit.Parent.Humanoid:TakeDamage(25)	
            pcall(function()
                   connection:Disconnect()
                   connection = nil
            end)
            delay(2.5, function()
                damageDebounce = false
            end)
	    end
    end
end

tool.LeftSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
        Connection = nil
	end)
end)

tool.RightSwing.OnServerEvent:Connect(function()
    local Connection
	Connection = tool.Hitbox.Touched:Connect(function(Hit)
		Damage(Hit, Connection)
        Connection = nil
	end)
end)

Sometimes disconnecting events get glitchy for some reason, at least for me.

This is very confusing, do you want the player to only ever take damage once by a player? So they could never damage them again?

I want the player to only be damaged when they get hit by a sword swing, so it doing damage, but not during a swing is an issue

This code didn’t work, is there any way to fully stop the touched event so that its not running until the event calls on it again?