Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

5 comments

0

Check if it's already anchored, and unanchor if it is. Otherwise, anchor it.

0

it is already unanchored

0

change line 2 to local h = Obj.Parent:FindFirstChild("Humanoid")

0

don't put it in a local script

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.

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.