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

Follow Script refuses to work? NPC just stands still.

Hello, I'm a little new to scripting on Roblox and I'm curious of how to fix my Follow script! (Makes the enemy follow and kill all humans) .

Mainly want help on that.. And for some reason people rather tell me to Kms usually then just help a fellow learning scripter.


1 answer

✓ Accepted answer

Hi!

I coincidentally have the same script you do, and for some reason it says "game.Workspace:children()". I don't know why that is, but it should be "game.Workspace:GetChildren()".

This is how the script should be.

function findNearestTorso(pos) --Function that finds the nearest player in range to the zombie
	local list = workspace:GetChildren() --Finds every child in the workspace.
	local torso = nil
	local dist = 1000 --Change range (in studs) here.
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do --For every child in the workspace do
		temp2 = list[x]
		if temp2:IsA("Model") and temp2.Name ~= script.Parent.Name then --The child is a model and isn't a zombie.
			temp = temp2:findFirstChild("HumanoidRootPart") --Find the player's torso.
			human = temp2:findFirstChild("Humanoid") --Find the player's humanoid.
			if temp and human and human.Health > 0 then --The player is still alive.
				if (temp.Position - pos).magnitude < dist then --The player's torso is in the zombie's range.
					torso = temp
					dist = (temp.Position - pos).magnitude --Find the distance between the player and zombie.
end end end end
	return torso
end

wait(1)

while wait() do
	local target = findNearestTorso(script.Parent.Torso.Position)--Calls the function
	if target ~= nil then --If the character's torso still exists then
		script.Parent.Humanoid:MoveTo(target.Position, target) --Move the zombie to the player
	end
end

Note: Make sure the NPC is unanchored, otherwise it will not follow. Also, for future questions, be sure to code block your code by selecting over it and clicking the Lua button.

4 comments

0

You're the best man, seriously. I'm glad there is people like you. I hope I can get as good as you someday haha

0

Although I am curious about one thing, the zombie does follow me now, but it doesn't want to follow me enough to where it attacks me, it only copies my movements

0

Oh oops, I was fondling with the script in a test server before I gave it to you and I had it where the NPC followed behind you. I have it fixed now where it will directly come in contact with your torso.

0

What the heck haha, it actually works. I don't know what to say man, I've been looking for this for ages and you answered me within a few minutes. You made my day

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