Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to make a block that anchors a NPC when you touch it?

Asked by 4 years ago

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)
0
Check if it's already anchored, and unanchor if it is. Otherwise, anchor it. DeceptiveCaster 3761 — 4y
0
it is already unanchored WheatMealLoaf -2 — 4y
0
change line 2 to `local h = Obj.Parent:FindFirstChild("Humanoid")` raid6n 2196 — 4y
0
don't put it in a local script krowten024nabrU 463 — 4y
0
to everyone who helped me figure this one out: Thank you so much! this was the last script I needed to finish my game. WheatMealLoaf -2 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)

Ad

Answer this question