Why does my sword script breaks?

I’m trying to make a sword that has 2 alternating animations.Thing is, I thought the damage was working fine until I tested it longer and it started to damage even if it was not activated yet.

Client:

local tool = script.Parent

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local remoteEvent = tool.RemoteEvent

local slash = tool.Slash
local slash2 = tool.Slash2

local slashTrack = animator:LoadAnimation(slash)
local slash2Track = animator:LoadAnimation(slash2)

local cooldown = false

local i = 1

tool.Activated:Connect(function()	
	if not cooldown then
		cooldown = true
		
		if i == 1 then
			i += 1
			
			slashTrack:Play()
		else
			i = 1
			
			slash2Track:Play()
		end
		
		remoteEvent:FireServer()
				
		wait(1)
		cooldown = false
	end
end)

Server:

local tool = script.Parent

local remoteEvent = tool.RemoteEvent

local handle = tool.Handle

local swing = handle.Swing
local hitSound = handle.Hit

local debounce = false

local slash

remoteEvent.OnServerEvent:Connect(function(player)
	swing:Play()

	slash = handle.Touched:Connect(function(hit)
		local character = hit.Parent
		local humanoid = character:FindFirstChild("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(character)
		
		if humanoid and not debounce then
			debounce = true
			
			humanoid:TakeDamage(20)
			
			hitSound:Play()
			
			wait(1)
			debounce = false
		end
		
	end)
	
	wait(1)
	slash:Disconnect()
end)

add a print in the touched event to see if it is hitting anything

edit: sorry I didn’t read it right, that isn’t the problem

1 Like

Your issue is a simple one indeed. It is the issue of a function that needs a debounce. If you don’t add a debounce to your function, it’s going to create more than one touched function that will you guessed it damage the player for every 1 second.

Here’s an alternate solution after reading your script:

local tool = script.Parent

local remoteEvent = tool.RemoteEvent

local handle = tool.Handle

local swing = handle.Swing
local hitSound = handle.Hit

local debounce = false

local slash

remoteEvent.OnServerEvent:Connect(function(player)
	swing:Play()
	if slash then
		slash:Disconnect()
	end
	slash = handle.Touched:Connect(function(hit)
		local character = hit.Parent
		local humanoid = character:FindFirstChild("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(character)

		if humanoid and not debounce then
			debounce = true
			humanoid:TakeDamage(20)
			hitSound:Play()
			wait(1)
			debounce = false
		end
	end)

	wait(1)
	slash:Disconnect()
end)
3 Likes

I’m not sure if I’m right about this or not, but if you run a remote a dozen of times it overwrites the function being connected. Say slash was once the first click, if you click again that touched function doesn’t disconnect but the one you just remade would.

1 Like

Yeah that is correct, nice catch.

1 Like