Pathfinding script working until it gets in a 5 stud magnitude, then when it gets out it still doesn't work. This script is supposed to stop when it gets into a 5 stud magnitude. It does, but when it gets out of that 5 stud magnitude it stops working.
repeat wait() until workspace:FindFirstChild("Player") local player1 = game.Workspace.Player local player = script.Parent game:GetService("PathfindingService").EmptyCutoff = 0 while (math.abs(script.Parent.Torso.Position.Magnitude - workspace.Player.Torso.Position.Magnitude)) > 5 do --Try that w = script.Parent.Torso.Position.Magnitude r = workspace.Player.Torso.Position.Magnitude print(math.abs(w-r)) local start = game.Workspace.NPC.Torso local fin = player1.Torso path = game:GetService("PathfindingService"):ComputeSmoothPathAsync(start.Position,workspace.Player.Torso.Position,400) points = path:GetPointCoordinates() divide = #points*5 for i, v in pairs(points) do player.Humanoid:MoveTo(v) wait(#points/divide) p = Instance.new("Part", workspace.Points) p.Size = Vector3.new(1,1,1) p.Position = v p.Anchored = true p.Name = "Point" p.CanCollide = false p.Transparency = 1 if pos then if pos < p.Position.y then player.Humanoid.Jump = false end end pos = p.Position.y end end
First off, Parts do not contain the property 'magnitude' and you can not get the distance between two points by supplying only one position (Only supplying one position gets the distance between that position and Vector3.new(0, 0, 0) )
You have to do something along the lines of (Pos1 - Pos2).magnitude
Secondly, when a while loop receives 'false' or 'nil' (which it does once the player is within the 5 stud radius), it supplies it's own break
Try making a conditional within the loop that runs the code if it is true.
while wait() do if 1 + 1 == 2 then DoStuff() end end
I don't think parts have magnitude. Magnitude is the distance between, so:
(math.abs(script.Parent.Torso.Position - workspace.Player.Torso.Position).magnitude)
http://wiki.roblox.com/index.php?title=Magnitude