I made a duplicate of my player and wanted to see if i could make a basic zombie. i looked up pathfinding on the wiki, i know how to use it. but... i need to know how to use magnitude to find out if which players is closest, and they need to be at least 15 studs away. here's what i got so far:
1 | local zombie = script.Parent |
2 | local pathfind = game:GetService( "PathfindingService" ) |
3 | local players = game.Players:GetPlayers() |
4 | while players.Character.UpperTorso.Position.magnitude < = 100 do |
5 | local Start = zombie.LowerTorso |
6 | end |
obviously this won't work.. i just gave that a shot.. and i can't really find any tutorials or anything on the wiki to help me use magnitude.
Try this, it should work.
01 | function FindNearest(position) |
02 | local lowest = math.huge -- infinity |
03 | local NearestPlayer = nil |
04 | for i,v in pairs (game.Players:GetPlayers()) |
05 | if v and v.Character then |
06 | local distance = v:DistanceFromCharacter(position) |
07 | if distance < lowest then |
08 | lowest = distance |
09 | NearestPlayer = v |
10 | end |
11 | end |
12 | end |
13 | return NearestPlayer |
14 | end |
15 |
16 | print (FindNearest(position)) -- change position to the position of the Zombie's upper/lower torso, or head. |
01 | function FindNearestPlayer(StartPlayer) |
02 | local found |
03 | local last = 0 |
04 | for i,v in pairs (game.Players:GetPlayers()) do |
05 | local distance = v:DistanceFromCharacter(StartPlayer.Character.HumanoidRootPart.Position) |
06 | if distance < last then |
07 | found = v |
08 | end |
09 | last = distance |
10 | end |
11 | return found |
12 | end |
should work
EDIT: If it doesnt work change the <
on line 6 to >