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

Does anybody know how to make it so that when an NPC touches a block, it cant move?

I want an NPC that stops moving when it touches a part of a gear, and when its not touching it moves fast again. The block I want it to slow down on is called "detect"

slow = true

script.Parent.Touched:Connect()(function(hit) 
	if hit.Parent:FindFirstChild("detect") then 
		script.Parent.Humanoid.WalkSpeed = 0
		slow = false
	end
end)

if slow = true
	script.Parent.Humanoid.WalkSpeed = 0
	else
	if slow = false
		script.Parent.Humanoid.WalkSpeed = 32
end

1 comment

0

You forgot to add "then" in the end of line 10 and 13.

2 answers

Do

script.Parent.Touched:Connect(function(hit)

instead of

script.Parent.Touched:Connect()(function(hit)

1 comment

0

Regardless of your answer, his code still contains several logical errors.

To break down the two errors that I saw in your code:

Line 3: Script.Parent.Touched:Connect()function(Hit) -- Your Function(Hit) is outside of the :Connect()'s Parenthesies, so the :Connect is not connecting the function to .Touched Line 4: if hit.Parent:FindFirstChild("detect") then -- you are finding the child "detect" when you are meant to look for the child "Humanoid" (I'll explain why later down)

Explanation on the hit:

When you do

part.Touched:Connect(Function(hit))
print(hit)

it will automatically print the names of the parts that touched it.

**My code: **

local part = script.Parent

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.WalkSpeed = 10
	end
	part.TouchEnded:Connect(function(hit)
		hit.Parent.Humanoid.WalkSpeed = 32
	end)
end)

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