Sword... multiplies damage?

I made a basic sword tool for an RPG I’m making with a friend, but for some reason, the damage multiplies itself. Here’s an example of what I mean:

The first hit does 15 damage, the second does 15 damage, but then the third does 30, the fourth does 30, etc, etc. The pattern continues if you keep going as well.

Sorry in advance for so many comments being in the scripts - my friend doesn’t know coding at all, so I wanted him to be able to understand it.

Here’s the client script:

t = script.Parent
hbox = t.Hitbox
han = t.Handle
cd = 2
onCd = false
ev = game.ReplicatedStorage.StarterSword

t.Activated:Connect(function() --when tool is used
	if onCd == false then
		print("setting touch") --for debugging
		local humanoidsHit = {} --make a table to store the people hit
		hbox.CanTouch = true --make sure it can actually HIT PEOPLE
		print("waiting...") --for debugging
		task.wait(0.002) --safety net so it doesn't try to run .Touched before touch is enabled
		hbox.Touched:Connect(function(hit)
			print("hit func ran")
			hbox.CanTouch = false --so it doesn't try to run .Touched multiple times
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum and not table.find(humanoidsHit, hum) then --check if it's a humanoid and if it's already in the table. if it is not in the table, continue
				print("hum. found") --for debugging
				print(hit) --for debugging
				print(hit.Parent) --for debugging
				print(hum) --for debugging
				table.insert(humanoidsHit, hum) --put humanoid in storage so you don't hit them twice
				print("inserted") --for debugging
				ev:FireServer(hum) --send signal to server
				print("passed to server")
			end
			task.wait(1) --delay for the length of the animation so it can calculate at ay point during it
			hbox.CanTouch = false --can no longer hit
			table.clear(humanoidsHit) --remove any stored ppl so you can hit them again
		end)
		onCd = true  --[
		wait(cd)
		onCd = false --] cd shenanigans
	end
	if onCd == true then return end
end)

And here’s the server script:

rs = game.ReplicatedStorage
strtr = rs.StarterSword

strtr.OnServerEvent:Connect(function(player, hum) -- recieves signal from client, had to add the stupid first variable or it tries to find the health of the player instead of character
	print("function recieved")
	if hum and hum:IsA("Humanoid") then -- ensure it's a humanoid
		print("hum detected")
		hum.Health -= 15 --deal damage
		print("hum damaged")
	end
end)

If anyone can help, thanks in advance!

This is because you are creating a new .Touched Event every time you activate the tool, meaning that everytime you use the sword, the damage will be multiplied as the the previously created events are not removed and thus are still active at the same time.

You can either have the touched event outside the function and control its output via the CanTouch, disconnect and properly dispose of the event before making a new one, or utilize queries via :GetBoundsInBox() to detect whether an object is within the bounds of your hitbox.

1 Like

The first solution worked. I had no clue you had to disconnect events… Thanks a lot!

1 Like