Script not identifying part properly

i have an enemy NPC. this script is to make the NPC play a slide animation once its head is hit by something in order to be able to move through spaces with low tops. there’s an if-then statement that detects if the hit part is NOT parented to the NPC model, but it doesn’t work.

every time there’s an animation that plays that involves the head touching a part of the body a bit, the slide animation unintentionally plays, which makes the NPC perform such an abomination of a dance that breaks all rules of the universe.

take note that i also tried setting the CanTouch property of some body parts to be false, but it didn’t work. some body parts also need to have CanTouch on at all times since they also need to detect touch for their own lil functions.

nothing in the dev console btw

local me = script.Parent
local hum = me.Humanoid
local anim = hum.slide
local animer = hum.Animator
local track = animer:LoadAnimation(anim)
local head = me.Head
local debounce = false

head.Touched:Connect(function(hit)
	if debounce == false then
		if hit.Parent ~= me then
			debounce = true
			me.patterns.Enabled = false
			track:Play()
			wait(track.Length)
			me.patterns.Enabled = true
			debounce = false
		end
	end
end)

Hello there,
This is not really an optimal solution by any means, but rather than re-invent your wheel I’m just gonna give you the solution; but I would really recommend looking into PlayerAdded and CharacterAdded events. Anyway…

This is the script I wrote to test this case and it worked for me perfectly fine.

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local head = Character:WaitForChild("Head")
local Debounce = false

head.Touched:Connect(function(HitPart)
	if Debounce == false then
		if HitPart.Parent ~= Character then
			warn("Valid Hit Detected")
			Debounce = true
			-- perform your function here
			Debounce = false
		end
	end
end)

Please use the code given above and put your slider and track variables in, then where it says perform your function here just put the track handling you’ve got in there.

A couple things to check:

  • Make sure the script is inside StarterCharacterScripts
  • Make sure the Players head is touch enabled
  • Make sure the Part is touch enabled
1 Like

oh. sorry but, my character isn’t a player. it’s a custom rig for an enemy in the workspace. it may have only worked for you because you used it for your own player. just to reassure, i’m trying to find out why my script is unable to detect whether or not the part the head is hitting is from the model or not so that the slide animation won’t play when other animations that involve the head clipping with other body parts are playing.

I see, you can use Part:IsDescendantOf(Character); not really performant but it’ll work

i tried it, and it also doesn’t work. i’m not sure if “not” is bugging out or smth, if it’s impossible to do what i’m tryna do, or if i’m doing it wrong.

head.Touched:Connect(function(hit)
	if debounce == false then
		if not hit:IsDescendantOf(me) then
			debounce = true
			me.patterns.Enabled = false
			track:Play()
			wait(track.Length)
			me.patterns.Enabled = true
			debounce = false
		end
	end
end)

Even if you are using a custom rig, as long as your head and the part are both touch enabled the script should work, could you try the debug script I gave and see if it prints (put it in startercharacterscripts, assuming your rig is a startercharacter it’ll work as intended)

ah. nevermind. there is completely nothing wrong with my script. it seems like what the head is detecting are shotgun shells that spawn in-front of the character when the character does a shooting animation. the character ducks forward in the animation, slamming the head into a mid-air shell. i solved it by disabling the CanTouch of the shotgun shells. thanks for trying to help tho. you also helped remind me that IsDescendantOf actually exists. :]

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