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.
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.