Sword Does Damage More Than Once

Try this one, if it doesn’t work then I’ve no idea how am I supposed to do this:

local tool = script.Parent
local damageDebounce = false

function Damage(Hit)
  if Hit.Parent:FindFirstChild("Humanoid") then
    if not damageDebounce then
      damageDebounce = true
      Hit.Parent.Humanoid:TakeDamage(25)	
      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)
    pcall(function()
      Connection:Disconnect()
      Connection = nil
    end)
    Connection = nil
  end)
end)

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

Still nothing, it still does damage after the swing when I don’t swing

Rexfiry, could you add me on discord? Kinda hard to help you thoroughly on the forums.

Why do you need to add me? (30 char)

I don’t have enough information from you here on the forums, we don’t know if the script errors or anything. Could you show a GIF or something?

There is no script errors and I linked a gif at the top of the post on exactly what happens, there is also another gif that I sent in the replies.

Alright sec, let me see what I can come up with.

local tool = script.Parent

function Damage(Hit)
	Hit.Parent.Humanoid:TakeDamage(25)	
end

tool.LeftSwing.OnServerEvent:Connect(function()
	local con
	con = tool.Hitbox.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			Damage(Hit)
			con:Disconnect()
		end
	end)
end)

tool.RightSwing.OnServerEvent:Connect(function()
	local con
	con = tool.Hitbox.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			Damage(Hit)
			con:Disconnect()
		end
	end)
end)

Give this a try.

Nope, doesn’t work. It doesn’t throw up any errors either.

Can you do print(con) after I disconnect it and see what it prints?

It prints “Connection” 8 times after one swing.

And this is you printing it after it is disconnected?

This is the exact code that I’m using

local tool = script.Parent

function Damage(Hit)
	Hit.Parent.Humanoid:TakeDamage(25)	
end

tool.LeftSwing.OnServerEvent:Connect(function()
	local con
	con = tool.Hitbox.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			Damage(Hit)
			con:Disconnect()
            print(con)
		end
	end)
end)

tool.RightSwing.OnServerEvent:Connect(function()
	local con
	con = tool.Hitbox.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			Damage(Hit)
			con:Disconnect()
            print(con)
		end
	end)
end)

What if you used a different form of checking if it touched a player? Maybe like GetTouchingParts or using a Raycast Hitbox melee module.

Or try this:

tool.RightSwing.OnServerEvent:Connect(function()
    local con
    con = tool.Hitbox.Touched:Connect(function(Hit)
        con:Disconnect()
        if Hit.Parent:FindFirstChild("Humanoid") then
            Damage(Hit)
            print(con)
        end
    end)
end)

Honestly, in my opinion, this is the most efficient way to use the touch function.

local rs = game:GetService(“RunService”)
local canHit = false
local sword = script.Parent.Handle

function damage()
	canHit = true
	wait(0.5)
	canHit = false
end

–// You can have your remote event wrapped around this function.

sword.Touched:Connect(function(hit)
	if canHit then
		local found 
		found = hit.Parent:FindFirstChild("Humanoid")
		if found then
			found:TakeDamage(20)found = nil 
		else
			print(found)
			canHit = false
		end
		rs.Heartbeat:Wait()
		canHit = false
	end
end)

Example:

He doesn’t want it like that, he only wants it to do damage if the player swings.

I stated inside of the code, that he can wrap his remote function around the touch function.

He already tried doing what you just did, didn’t work.

Well not that exact code, slight differences, but I doubt it’ll help.