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

Pathfinding script not working?

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

2 answers

✓ Accepted answer

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.


###Example

while wait() do
	if 1 + 1 == 2 then
		DoStuff()
	end
end

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