Script deals multiple ticks of damage no matter what I do?

Heya!

I’m trying to make a sword script from scratch, as me and a friend are collaborating to make an RPG game. However, no matter what I do, it seemingly won’t stop having the same bug: the script deals damage multiple times before the function ends.

t = script.Parent
hbox = t.Hitbox
han = t.Handle

t.Activated:Connect(function()
	print("setting touch")
	hbox.CanTouch = true --make sure it can actually HIT PEOPLE
	print("waiting...")
	task.wait(0.002) --make sure it doesnt blow up
	hbox.Touched:Connect(function(hit)
		print("turning off the freaking disentegration")
		hbox.CanTouch = false --did it hit smth?
		print("touch fired")
		task.wait(0.25)
		if hit.Parent:WaitForChild("Humanoid") ~= nil then --check to make sure we arent killing the AIR
			print("it exists!")
			local hum = hit.Parent:WaitForChild("Humanoid")
			hum.Health = hum.Health - 15 --actual damage
		end
	end)
	task.wait(1) --delay so hit detection lasts the whole animation
	if hbox.CanTouch == true then hbox.CanTouch = false end -- check then make sure it doesnt kill everything
end)

BEFORE YOU SAY ANYTHING ABOUT IT NOT DAMAGING ON THE SERVER BECAUSE I DON’T HAVE EVENTS: I know. I’m testing it on local before I set up events.

…unless that’s the problem, in which case, I’m sorry.

Edit: Everything prints in console fine, besides spamming and clogging the output.

Try doing a debounce, just incase it’s hitting the character multiple time:

local humanoidsHit = {}

hbox.Touched:Connect(function(hit)
	hbox.CanTouch = false

	task.wait(0.25)

	local hum = hit.Parent:FindFirstChild("Humanoid")

	if hum and not table.find(humanoidsHit, hum) then
		table.insert(humanoidsHit, hum)

		hum.Health -= 15
	end

	task.wait(1)

	hbox.CanTouch = false

	table.clear(humanoidsHit)
end)

This worked perfectly! Thanks a lot.

I had no clue tables could be used this way! You learn something new every day, I guess. Thanks, kind stranger!

1 Like

I would use a lookup table here

local humanoidsHit = {}

hbox.Touched:Connect(function(hit)
	hbox.CanTouch = false

	task.wait(0.25)

	local hum = hit.Parent:FindFirstChild("Humanoid")

	if hum and not humanoidsHit[hum] then
		humanoidsHit[hum] = true

		hum.Health -= 15
	end

	task.wait(1)

	hbox.CanTouch = false

	humanoidsHit = {}
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.