Touched event not working

SETTING THE STAGE: An NPC with a sword that slashes and when it hits a hit box damage gets taken away, only that the touched event fires 3 times every debounce turn

  1. What do you want to achieve? Keep it simple and clear!
    I want the touched event to work properly

  2. What is the issue? Include screenshots / videos if possible!
    I’ve tested the debounce and it works perfectly fine, but in the touched event I put a if statement for a specific part that that fires like 3 times every next debounce turn that only fires once.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    All the solutions I found said add a debounce which I already had

T
snippet:

		
		function swing()
		
	if sword_debounce == false then
		sword_debounce = true
			local animation = humanoid:LoadAnimation(script.Animation)
		local damage = true
		animation:Play()
		animation.Stopped:connect(function()
			damage = false
				wait(0.5)
			sword_debounce = false

		end)
        
			local damged_humanoids = {}
			
			
			

			NPC.Sword.Part.Touched:Connect(function(hit)
				
				
				
				if hit.Name == "HitBox" then
					print(hit)
				end
				
				if hit.Name == "HitBox" then
					
					script.Parent.Sword.Blade.SwordSlash:Play()
					
						local thing = hit.Parent.Parent:FindFirstChildWhichIsA("Humanoid")
					if thing then
						thing:TakeDamage(5)
					end
			    end
				if hit.Parent:FindFirstChild("RedHealthg2") and _G.healthGreen > 0 and damged_humanoids[hit.Parent.Name] == nil then
					script.Parent.Sword.Blade.SwordSlash:Play()
					_G.healthGreen -= 0.2
					
					damged_humanoids[hit.Parent.Name] = true
				end
				if hit.Parent:FindFirstChild("RedHealthb2") and _G.healthBlue > 0 and damged_humanoids[hit.Parent.Name] == nil then
					script.Parent.Sword.Blade.SwordSlash:Play()
					_G.healthBlue -= 0.5

					damged_humanoids[hit.Parent.Name] = true
				end
				if hit.Parent:FindFirstChild("RedHealthBL2") and _G.healthBlack > 0 and damged_humanoids[hit.Parent.Name] == nil then
					script.Parent.Sword.Blade.SwordSlash:Play()
					_G.healthBlack -= 0.2

					damged_humanoids[hit.Parent.Name] = true
				end
			
		end)
	end
	end
3 Likes

Is the damage line inside the if statement?

thought i should make it simpler but there i put the whole thing

The if statement isn’t actually inside the touched event :I

but it is wdym the if hit.Name == “HitBox” then is inside

I meant the debounce if statement isn’t inside the touched event.
Try putting the debounce if statement inside the touched event instead of outside

1 Like

wdym no there is an outer if statement that has a debounce which only fires 0.5 secs which works i tested

image

do you want me to create another deb because i cant move that there it will mess up the animation

If you replace/invert the positions correctly, it won’t mess up. Just cut the touched event, place it outside of the debounce if statement, cut the debounce if statement into the touched event, and it’s that easy

Edit: also the answer to your question is yes, you could do that but it’s less efficient

1 Like

What’s happening is the function is getting called every time the hitbox is hit, but when the original hit is created you’re not checking to see if the debounce is true or false there, so it fires every time. You don’t have the debounce where the hit occurs, you have it in a separate function.

This will trigger when the animation stops … but the code will keep going.
I think this should be: animation.Stopped:Wait()

1 Like

where does the animation go then like so no deb in the animation?

Everything inside the touched event should be inside the debounce if statement and everything inside the if statement should still be inside the if statement.

1 Like

had to take a break my head was on fire for some reason came back everything makes sense I’m good now thx dude

3 Likes

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