Why is my sword still doing dmg after disconnecting function?

I created a katana, and when the player clicks it deals damage, it should stop dealing damage to things it touches when the player is not slashing, for some reason it still does even after I disconnect the function.

Here’s my code:

wait(1)

local Remote = script.Parent.Katana_Remote
local Tool = script.Parent
local Blade = script.Parent.Blade
local BladeHit

Remote.OnServerEvent:Connect(function(playerWhoFired)
	
	Tool.Activated:Connect(function()
	
		BladeHit = Blade.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid ~= Tool.Parent.Humanoid then
				hit.Parent.Humanoid:TakeDamage(10)
			end
			wait(1)
			BladeHit:Disconnect()
		end)

	end)
	
end)

Any help is appreciated.

There are obviously two listeners you have to disconnect.

your connecting a new function to the touched event every time you get a remoteevent

Nested connections, you need to appropriately disconnect the Tool.Activated connection.

@5uphi
@Forummer
Can you elaborate? Do you mean Its wrapped in a double function therefore I have to disconnect both?

Each time the outer OnServerEvent event is fired the tool’s Activated event is connected to a new lambda function.

So I should wrap the Remote.OnServerEvent function inside of the Tool.Activated?

The problem still occurs without the Tool.Activated function.

What’s the other listener? The Remote or the Tool.Activated?

Tool.Activated. However, you shouldn’t be using that on the server??

Without that the problem still occurs…

Remote.OnServerEvent:Connect(function(playerWhoFired)
	
	BladeHit = Blade.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid ~= Tool.Parent.Humanoid then
			hit.Parent.Humanoid:TakeDamage(10)
		end
	end)
	
	wait(1)
	BladeHit:Disconnect()
	
end)

Well you are waiting 1 whole second. Is it doing damage after 1 second?

Yes. (I the hit character limit of 30)

local touchedConnection = nil
local attacking = false
local debounce = {}

-- this function will damage characters by 10 when it hits any of there character parts
local function Touched(touchedPart)
	local humanoid = touchedPart.Parent:FindFirstChild("Humanoid")
	if humanoid == nil then return end
	if humanoid == Tool.Parent.Humanoid then return end
	if debounce[humanoid] == true then return end
	debounce[humanoid] = true
	humanoid:TakeDamage(10)
	task.wait(1)
	debounce[humanoid] = nil
end

Remote.OnServerEvent:Connect(function()
	-- if the player is already attacking then exit this function early
	if attacking == true then return end

	-- set attacking to true to stop the player from attack again
	attacking = true

	-- start listening for touches
	touchedConnection = Blade.Touched:Connect(Touched)
	
	-- wait 1 second
	task.wait(1)

	-- stop listening for touches
	touchedConnection:Disconnect()

	-- set attacking back to false to allow the player to attack again
	attacking = false
end)

I believe since BladeHit is out of the remote listener’s scope then it’s being rewritten as a different connection therefore the previous connection is lost. (define BladeHit inside the OnServerEvent listener)

Yea, I believe that fixed the problem.

Remote.OnServerEvent:Connect(function(playerWhoFired)
	
	local BladeHit = Blade.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid ~= Tool.Parent.Humanoid then
			hit.Parent.Humanoid:TakeDamage(10)
		end
	end)
	
	wait(1)
	BladeHit:Disconnect()
	
end)

Thanks.