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

I need help with pathfinding , Im really bad at it. Can anyone help?

while wait(0.1) do

path = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Torso.Position,game.Workspace.MagnTest.Position,511)
points = path:GetPointCoordinates()

print(points)

game.Workspace.Points:ClearAllChildren()
local xde = nil

for p = 1,#points do
	part = Instance.new("Part")
	part.FormFactor = Enum.FormFactor.Symmetric
	part.CanCollide = false
	part.Size = Vector3.new(1,1,1)
	part.Position = points[p]
	part.CFrame = part.CFrame - Vector3.new(0,3,0)
	part.Anchored = true
	part.BrickColor = BrickColor.DarkGray()
	part.Parent = game.Workspace.Points
	part.Name = "part"..p..""
	xde = "part"..p 
end

local zaa = game.Workspace.Points:GetChildren()

for i = 3,#zaa do
	script.Parent.Zombie:MoveTo(game.Workspace.Points:FindFirstChild("part"..i).Position,game.Workspace.Points:FindFirstChild("part"..i)) 
	local mag1 = script.Parent.Torso.Position - game.Workspace.Points:FindFirstChild("part"..i).Position
	local mag = mag1.Magnitude
	repeat wait() print(mag) until  mag <= 10
end end

Zombie walks to the first part and then just stops.

If you're going to test this in your place make sure you put an empty model called "Points" first.

1 answer

✓ Accepted answer

To start, I highly suggest you try to use more descriptive variable names. xde and zaa doesn't really say a ton about what the variable does. Also, you would probably be better off directly using the points table directly, rather than creating a new Model of Parts and iterating through that.

Despite those minor issues, the main problem is in these lines:

local mag1 = script.Parent.Torso.Position - game.Workspace.Points:FindFirstChild("part"..i).Position
local mag = mag1.Magnitude
repeat wait() print(mag) until  mag <= 10

You only calculate the distance to the next point one time, and then expect it to change within the repeat loop. You have to put the magnitude calculation inside the loop:

local torso = script.Parent.Torso
local point = workspace.Points:FindFirstChild("part" .. i)
repeat wait() until (point.Position-torso.Position).magnitude <= 2 -- Also, 10 studs is a little far for these purposes, so I bumped it down to 2

Hope this helps -- let me know if you need tips on making this code look and perform more streamlined.

1 comment

0

I see what you mean , I did it , didn't work at the start but once I changed minimum magnitude to 4 from 2 it worked like a charm, thanks!

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