I'm trying to make a zombie that comes after you once you're 50 studs away from it. However I do not know how to check how far away my character is from the zombie. I know there is a method for it, but I do not know that method. Here's what i have so far.
This is a server script inside the zombie.
local zombie = script.Parent function getNearestTorso() local nearestTorso = nil for i,v in pairs(game.Player:GetPlayer()) do if [distance from plr to zombie] <= 50 then if nearestTorso ~= nil then if [distance from nearestTorso to zombie] > [distance from plr to zombie] then nearestTorso = v.Character.Torso end else nearestTorso = v.Character.Torso end end end return nearestTorso end while true do wait(3) if getNearestTorso() ~= nil then local tor = getNearestTorso() zombie.Humanoid:MoveTo(tor.Position,tor) end end
Try this
function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 50 local temp = nil local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("Torso") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end
I don't see why people keep using this lame free model michael suggested...I created a follow script that is 6 lines long and does exactly the same thing.
Zeptix just use magnitude and then make the zomie go to the player using the MoveTo method like so:
--Example: if (brick1.position - brick2.position).magnitude <= followDistance then MoveTo(blablabla) end
I suggest you put everything (except for the variables) inside a while loop, the script will be shorter.