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

(Answered not on website)How do I make an npc avoid obstacles and walk to a position?

So I have this code in a script inside an npc:

local pathfindingService = game:GetService("PathfindingService")
local pointModel = Instance.new("Model")
pointModel.Name = "Points"
pointModel.Parent = game.Workspace
 
function visualizePath(path)
	-- clear old path visualization
	pointModel:ClearAllChildren()
 
	-- calculate new visualization	
	local points = path:GetPointCoordinates()
	for _, point in ipairs(points) do
		local part = Instance.new("Part")
		part.Parent = pointModel
		part.FormFactor = Enum.FormFactor.Custom
		part.Size = Vector3.new(1,1,1)
		part.Position = point
		part.Anchored = true
		part.CanCollide = false
	end
end
 
function walkto(target, npc)
	local start = npc.Torso.Position
	local path = pathfindingService:ComputeRawPathAsync(start, target, 500)
 
	visualizePath(path)
	-- Check to see if the path was computed correctly
	if path.Status == Enum.PathStatus.FailStartNotEmpty or path.Status == Enum.PathStatus.FailFinishNotEmpty then
		print("Compute failed")
		return {}
	end
 
	return path:GetPointCoordinates()		
end

I should be able to make the npc move with this code:

position = Vector3.new(0,0,0)
pos = walkto(position, script.Parent)
for _,pos in ipairs(pos) do
	script.Parent.Humanoid:MoveTo(pos)
end

However the npc doesn't seem to move at all, and if I add a print in, it prints for as many points as their are(like it should) but its prints them all almost immediately. All I need is for an npc to walk to a position, while avoiding obstacles so it doesn't get stuck. All help is appreciated :)

Note: The trail of parts using visualizePath works.

For kingdom5:

wait(3)
math.randomseed(tick())
local npcModel = script.Parent
local pfServ = game:GetService('PathfindingService') 
local start = npcModel.Torso
local tycoons = {}
for _,object in ipairs(game.Workspace:GetChildren()) do
	if(object.Name == "Tycoon") then
		table.insert(tycoons, object)
	end
end

local stop = tycoons[math.random(#tycoons)]:FindFirstChild("orderhere")

function visualizePath(path) 
	-- calculate new visualization	
	local points = path:GetPointCoordinates()
	for _, point in pairs(points) do
		local part = Instance.new("Part", game.Workspace)
		part.FormFactor = Enum.FormFactor.Custom
		part.Size = Vector3.new(1,1,1)
		part.Position = point
		part.Anchored = true
		part.CanCollide = false
	end
end


function walk(path)
	for i, v in ipairs(path:GetPointCoordinates()) do
		visualizePath(path)
		print('moving to', v)
		npcModel.Humanoid:MoveTo(v)
		if not npcModel.Humanoid.MoveToFinished:wait() then
			return false
		end
	end
	return true
end
function run()
	local path = pfServ:ComputeRawPathAsync(start.Position, stop.Position, 500)
	if path.Status == Enum.PathStatus.Success then
		while not walk(path) do
			path = pfServ:ComputeRawPathAsync(npcModel.Torso.Position, stop.Position, 500)
		end
	else
		print('path could not be found', path.Status)
	end
	return true
end

wait(10)
run()

I got the npc which was causing a problem as a free model. All I did to fix the problem was to use a different free model npc.

2 comments

0

If you found a solution to your problem then please consider writing an answer yourself.

0

Will do

1 answer

For your prints not to happen immediately, in the second part of code add this in:

script.Parent.Humanoid.MoveToFinished:wait()

So then it will end up with:

position = Vector3.new(0,0,0)
pos = walkto(position, script.Parent)
for _,pos in ipairs(pos) do
    script.Parent.Humanoid:MoveTo(pos)
    script.Parent.Humanoid.MoveToFinished:wait()
end

Now, for your thing not moving. Check if the "pos" is actually returning something that the Humanoid can use to MoveTo.

Anyways, Good luck!

4 comments

0

The npc still does not move. And I know the points are fine, because the visualizePath works.

0

Also, I see in the npc, the walktopoint has changed, yet the npc is not moving!

0

Is your NPC in a movable position? Maybe its accidently anchored?

0

Its not anchored although the humanoid says its anchored an I can't unanchor it, but I think kingdom5 has working code, so getting his code.

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