My sword accumulates damage, how can I fix it?

Hi, I have been developing a sword for my game, but I have a problem.
Basic attack damage is stacking and I don’t know how to fix it, could you help me please
robloxapp-20210822-2113466.wmv (2.2 MB)
? I give you a video of the problem and the code.

local remote = script.Parent.Attack 

remote.OnServerEvent:Connect(function(Player, Value) 
	local character = workspace:WaitForChild(Player.Name) 
	
	if Value == "Attack" then 
		
		local avaibleAttackAnimations = {7292031028, 7289385180, 7305181383} 
		
		local animation = Instance.new("Animation", character) 
		animation.Name = "AttackAnimation" 
		animation.AnimationId = "rbxassetid://"..tostring(avaibleAttackAnimations[math.random(1, #avaibleAttackAnimations)]) 
		local loader = character:WaitForChild("Humanoid"):LoadAnimation(animation)
		loader:Play()
		
		local alreadyHitObjects = {} 
		
		script.Parent.Parent.Katana1.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= Player.Name then 
				for _, alreadyHit in pairs(alreadyHitObjects)do 
					if alreadyHit == hit.Parent.Name then 
						return
					end
				end
				
				alreadyHitObjects[#alreadyHitObjects + 1] = hit.Parent.Name 
				
				hit.Parent.Humanoid:TakeDamage(10) 
			end
		end)
	end
end)

Likely because you don’t disconnect the touched event after sometime. It sitll keeps the event and will keep making them for how many times you call the RemoteEvent

A quick fix would just be to make the event disconnect after sometime

Example

local event
event = yourEvent:Connect(somefunction)

task.wait(5)

event:Disconnect()

Would disconnect the event after 5 seconds and prevent it from listening anymore.

Though I think you should use a keyframe in your animation that has a different name that when it is reached, would get all the parts touching the sword and damaging the first enemy it finds (or all of them if you want). You’d need to use AnimationTrack.KeyframeReached for this, or Animation events (very bottom of article) or you would want to use that instead

Thank you! Now it works well
:slight_smile:

1 Like

I had the same issue. I tried disconnecting the events, returning them but nothing worked. So I decided to use RayCasting instead of touched event and it works perfectly now.

local remote = script.Parent.Attack 

remote.OnServerEvent:Connect(function(Player, Value) 
	local character = workspace:WaitForChild(Player.Name) 

	if Value == "Attack" then 

		local avaibleAttackAnimations = {7292031028, 7289385180, 7305181383} 

		local animation = Instance.new("Animation", character) 
		animation.Name = "AttackAnimation" 
		animation.AnimationId = "rbxassetid://"..tostring(avaibleAttackAnimations[math.random(1, #avaibleAttackAnimations)]) 
		local ray = Ray.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.CFrame.LookVector * Vector3.new(10,10,10)) --10 is the lenght of the sword
		local hit, position = workspace:FindPartOnRay(ray, character, false, true)	
		local loader = character:WaitForChild("Humanoid"):LoadAnimation(animation)
		loader:Play()

		if hit then
			local hum = hit.Parent:FindFirstChild("Humanoid") 
			if hum then
				hit.Parent.Humanoid:TakeDamage(10) 
			end
		end
	end
end)
1 Like