So if the title didnt make sense, basically i want to touch a part named AttackTrigger so I can unanchor an npc called bob. Here's the code that doesnt work:
function onTouched(Obj) local h = workspace.Obj.Parent:FindFirstChild("Humanoid") if h then workspace.Bob.HumanoidRootPart.Anchored = true end end script.Parent.Touched:Connect(onTouched)
I couldn't reproduce your problem efficiently, but I'll try my best.
I assume you have already put the script inside the part and it's a normal script
.
There's a small mistake you made in the second line.
local h = workspace.Obj.Parent:FindFirstChild("Humanoid")
The Obj
is a variable passed by the native function of the part Touched
.
According to the API reference, the passed variable is BasePart
.
It means that you don't need to type workspace.
before typing Obj
because Obj
is already a variable.
local h = Obj.Parent:FindFirstChildWhichIsA("Humanoid")
Also, sometimes(Just sometimes) using class finder is better for unexpected
names if you set the name of the player's Humanoid
to a different name...
Using print
is always a good practice to debug!
function onTouched(Obj) local h = Obj.Parent:FindFirstChildWhichIsA("Humanoid") if h then workspace.Bob.HumanoidRootPart.Anchored = true end end script.Parent.Touched:Connect(onTouched)