Sword only damages once every two clicks

I’m trying to make a sword but the thing is, for some reason, it only damages once every two .Activated. Please help, thanks much!

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(cooldown)
		
		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 slash

remoteEvent.OnServerEvent:Connect(function(player, cooldown)
	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 cooldown then		
			cooldown = false
			
			humanoid:TakeDamage(20)
			
			hitSound:Play()
		end
		
		slash:Disconnect()
	end)
end)

I think you should just send a RemoteEvent as soon as the tool is activated and do everything on the server, just send over the cooldown.

can you please elaborate? thank you

So I would have a system where on the sword you would have all the Properties of the sword so the cooldown, the damage, etc

local tool = script.Parent -- Script Should be in tool

tool.Activated:Connect(function()
    Event.FireServer(Damage, Cooldown, --etc)
end)

-- On server script

Event.OnServerEvent:Connect(function(player, damage, cooldown)
     local humanoid = player.Character.Humanoid -- Really basic way of getting humanoid, wouldn't recommend doing it this way.
     if humanoid then
        local animation = humanoid:LoadAnimation(--Animation)
        anim:Play()
        -- If you have sound then do Sound:Play() Make sure to define the where the sound is
        wait(cooldown)
        anim:Stop()
    end
end)

By no means is this the best way, this just the really basic way of doing it. I would recommend adapting the way I have done in your own way, and add more checks so it can’t easily be exploited. As for the damage, check to see if the part of the weapon was touched and who it was touched by and then Simply do humanoid:TakeDamage(damage) making sure that the humanoid exist and is not the player holding the sword.

1 Like