1

# Why doesn't this script make my NPC jump when it touches a part?

So my script is supposed to make the NPC jump when it touches a part but not when it touches a part in a player but it doesn't work. Can anyone help me out?

local zombo = script.Parent.Parent:WaitForChild("Zombie") -- Name of NPC's Humanoid

zombo.Touched:Connect(function(Hit)
if Hit.Parent.Name ~= {"Torso","Left Arm","Right Arm","Left Leg","Right Leg","Head"} then
zombo.Jump = false
else
zombo.Jump = true
end
end)


0
Goulstem 8144
3 years ago
Edited 3 years ago

An easy way to check if it was a character that touched is to see if Hit.Parent contains a Humanoid object. You can do this with the FindFirstChild function. The logic behind this:

'Hit' --> the object that touched(potential limb/torso/rootpart)

'Hit.Parent' --> the object's parent, OR potential Character model

'Hit.Parent.Humanoid' --> nil, OR potential Character's Humanoid.

You can also shorten this whole code up using boolean logic. Set the 'Jump' Property directly correlating to the search of Hit.Parent's potential Humanoid. Use the not operator to indicate opposition.

local zombo = script.Parent.Parent:WaitForChild("Zombie")

zombo.Touched:Connect(function(Hit)
zombo.Jump = not Hit.Parent:FindFirstChild("Humanoid")
--If hit's parent has a humanoid, zombie won't jump.
--if hit's parent doesn't have a humanoid, zombie will jump.
end)


I also would consider setting up a debounce for this.