How would I make this MoveTo into pathfinding?

So my NPC goes through walls and is dumb? how can I make this into pathfinding? I have no idea. I’ve read the wiki on the pathfinder but no clue.

local larm = script.Parent:FindFirstChild("HumanoidRootPart")

local selectedPlayer

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					selectedPlayer = temp
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end



while true do
	wait(math.random(1,5))
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end

end

May I recommend pathfinding service?

its litterly what he wanted, a way to turn it into pathfinding :skull:

try this

while true do
	wait(math.random(1,5))
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target.Position)
	end

end

That’s basically what I’m doing. it doesnt change anything. the npc walks towards me but it cant solve the maze

1 Like

Oh try :MoveToFinished instead of :MoveTo

I’m trying to use the path finding service, not the moveto function. i just want to convert my moveto code into pathfinding

Take a look at this comment. It does what you asked.

Here is the code

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath{
	AgentRadius = 2.0,
	AgentHeight = 5.0,
	AgentCanJump = false
}

local waypoints
local arrival = 0
local oldtime = tick()

local character = script.Parent --- ur character
local Cx, Cz =  character.PrimaryPart.Position.X,  character.PrimaryPart.Position.Z -- Your Center Point
local radius = 50 -- Your Radius
local stayTime = 4 -- How Long you want the character to wait at destination (Seconds)

local PathParts = {}


function drawCircle() -- DELETE IF YOU WANT THE CIRCLE TO NOT SHOW
	for i = 0, 360, 5 do
		local angle = (i * math.pi) / 180
		local x, y, z = math.cos(angle ) * radius , 0, math.sin(angle) * radius
		local part = Instance.new("Part",workspace)
		part.Position = Vector3.new(x,y,z)
		part.Anchored = true
		part.Size = Vector3.new(1,1,1)
		part.Material = Enum.Material.Neon
		part.CanCollide = false
	end
end

function drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
	local part = Instance.new("Part",workspace)
	part.Position = waypoint.Position
	part.Anchored = true
	part.Size = Vector3.new(1,1,1)
	part.Material = Enum.Material.Neon
	part.CanCollide = false
	part.Color = Color3.fromRGB(255, 0, 0)

	table.insert(PathParts, part)
end

local function followPath(character, destination)
	local humanoid = character.Humanoid
	
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = path:GetWaypoints()
		local distance = (character.PrimaryPart.Position - waypoints[#waypoints].Position).Magnitude
		arrival = (distance/humanoid.WalkSpeed) + stayTime
		
		for i,waypoint in pairs(waypoints) do
			drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:wait()
		end
	end
end

drawCircle() -- DELETE IF YOU WANT THE CIRCLE TO NOT SHOW

while wait() do
	local angle = math.random(0,2*math.pi)-- Random angle in radians
	local x, y, z = math.cos(angle ) * radius , character.PrimaryPart.Position.Y, math.sin(angle) * radius -- end poistion
	local humanoid = character.Humanoid --- ur humanoid
	
	if tick() - oldtime > arrival  then
		
		for i,v in pairs(PathParts) do
			v:Destroy()	
		end
		
		followPath(script.Parent,  Vector3.new(x,y,z))	
		isReturned = false
		oldtime = tick()
	end
end

Yeah thats how i want to to work but how would i get the nearest player and set the target to that okayer?

This is a bit of spoonfeeding but here


local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath{
	AgentRadius = 2.0,
	AgentHeight = 5.0,
	AgentCanJump = false
}

local waypoints
local arrival = 0
local oldtime = tick()

local character = script.Parent --- ur character
local Cx, Cz =  character.PrimaryPart.Position.X,  character.PrimaryPart.Position.Z -- Your Center Point
local radius = 50 -- Your Radius
local stayTime = 4 -- How Long you want the character to wait at destination (Seconds)

local PathParts = {}
function drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
	local part = Instance.new("Part",workspace)
	part.Position = waypoint.Position
	part.Anchored = true
	part.Size = Vector3.new(1,1,1)
	part.Material = Enum.Material.Neon
	part.CanCollide = false
	part.Color = Color3.fromRGB(255, 0, 0)

	table.insert(PathParts, part)
end

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					selectedPlayer = temp
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end


local function followPath(character, destination)
	local humanoid = character.Humanoid
	
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = path:GetWaypoints()
		local distance = (character.PrimaryPart.Position - waypoints[#waypoints].Position).Magnitude
		arrival = (distance/humanoid.WalkSpeed) + stayTime
		
		for i,waypoint in pairs(waypoints) do
			drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:wait()
		end
	end
end



while wait() do
	local angle = math.random(0,2*math.pi)-- Random angle in radians
	local humanoid = character.Humanoid --- ur humanoid
	
	if tick() - oldtime > arrival  then
		
		for i,v in pairs(PathParts) do
			v:Destroy()	
		end
		
		followPath(character,  findNearestTorso())	
		isReturned = false
		oldtime = tick()
	end
end

Workspace.Creature.Pathfinding2:46: attempt to perform arithmetic (sub) on Vector3 and nil

The last value is nil? do I need to delay the script?

no can u send me the game file

monophobia.rbxl (119.2 KB)

Here. Also can I use this in my portfolio (the script since I made it)
monophobia.rbxl (125.9 KB)

2 Likes

Thank you so much. Sure go ahead you can use it

2 Likes