Having trouble figuring out how to get my touch event to work

Hello I’am working on making a method for damage output in my game.
image

I’m trying to make it so that if the hitbox on the sword, comes in contact with the EnemyHurtBox of the npc, it will print into the output as “Strucked”

But it does not seem to be working as it does not show up in the output

image

print("Hello world!")

local HitBox = script.Parent

local debounce = false

HitBox.Touched:Connect(function(Hit)

if debounce then return end

debounce = not debounce

if Hit.Parent:FindFirstChild("WeaponHurtBox") then

print("Strucked")

end

end)

Here is my script that I have inside the EnemyHurtBox. I have tried a lot of methods that i could find and none of them seem to work.

1 Like

Add a print above the debounce if statement and see if that’s firing. Also print Hit.Parent and check if there’s a child called ‘WeaponHurtBox’.

image
Both Prints are firing
The ‘Hit.Parent’ print is printing my name.

And inside of your character, there’s something called ‘WeaponHurtBox’?

Yes…sort of, The Sword is Parented and welded to my character’s right hand, and the WeaponHurtBox is in the sword model that is in my character. image

It has to be a direct child of the Hit.Parent variable, in your case would be your character. You could try and use :IsDescendantOf instead?

Oh you are using a hitbox like this. i recomend you to use this HitBox from @TeamSwordphin Raycast Hitbox 4.01: For all your melee needs!

1 Like

You’re making the debounce always true since you don’t make debounce = false on the end of strucked

Wrong. debounce will be false.

There’s also this line which blocks that command from happening anyway;

debounce is false by default. so wrong.

Don’t say I’m wrong again please.

It will only run the first time the .Touched event happends and never again; if it’s inside a code block he should be doing :Disconnect()

It gets set to true and then never again

1 Like
  • You aren’t resetting your debounce. At the end of the if statement, put debounce = false
  • if debounce then return end isn’t necessary. Just change the second if statement to if debounce == false and Hit.Parent:FindFirstChild("WeaponHurtBox") then and remove if debounce then return end
  • debounce = not debounce works, but I usually use debounce = true (Optional revision.)

Revised Script:

local HitBox = script.Parent --defines HitBox

HitBox.Touched:Connect(function(Hit) --BasePart.Touched event
   if Debounce == false and Hit.Parent:FindFirstChild("WeaponHurtBox") then --Makes sure debounce is false and Hit.Parent has a WeaponHurtBox.
Debounce = true --Sets debounce to true so the touched event doesn't spam.
      print("Strucked")
--do extra stuff
    end
end)
2 Likes