Help with Pathfinding

I am trying to make a game called Build a Store! I need to make customers. Anyone know how they make the customers in the Big games production “My Restaurant?” I want to make something like this:


Here is my code: (please note: when the player joins, whatever kind of store they have selected puts into a folder called “Vacant” in game.Workspace.)


while true do
	
	-- Loop --
	
	wait(5)
	
	--// Services \\--
	
	local PathFindingService = game:GetService("PathfindingService")
	
	--// Varibles \\--
	
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local NPCFolder = ReplicatedStorage:WaitForChild("NPCS"):GetChildren()
	local NPC = NPCFolder[math.random(1, #NPCFolder)]:Clone()
	
	-- Main Code --
	
	NPC.Parent = game.Workspace
	
	-- The storesAval varible is because I have a folder in Workspace called vacant 
	-- Meaning that when a player gets a plot, it it put to vacant

	local storesAval = game.Workspace.Vacant:GetChildren()
	
	local EndPosition = storesAval[math.random(1, #storesAval)].SpawnPart.Position
	
	NPC.HumanoidRootPart.Position = EndPosition
	
	local Humanoid = NPC.Humanoid
	print(NPC.Parent.Name)

	local EndDestination = storesAval[math.random(1, #storesAval)].Milk

	local Path = PathFindingService:CreatePath()
	Path:ComputeAsync(NPC.HumanoidRootPart.Position, EndDestination.Position)

	if Path.Status == Enum.PathStatus.Success then
		local Nodes = Path:GetWaypoints()

		for i,v in pairs(Nodes) do
			local node = Instance.new("Part")
			node.Size = Vector3.new(1,1,1)
			node.Position = v.Position
			node.Anchored = true
			node.CanCollide = false
			node.Transparency = 1

			Humanoid:MoveTo(v.Position)
			Humanoid.MoveToFinished:Wait(1)
		end
	end
end

The characters do not show up, but glitch out of the map or go somewhere else. How do I make them start at the start point, and end at the right point? I also want the character to walk out and give the player cash which is in leaderstats, but if you want to help you can.

Thanks.

EDIT: PLEASE ASK IF YOU NEED THE GAME FILE

1 Like

Can someone help me? I have been unable to get a response.

Does the npc start at the start position? Also do you know if the pathfinding part actually works? Sorry, I’m not too good at pathfinding

Yeah, in the code, the “NPC.HumanoidRootPart.Position” sets the position to make it at the start position, then it runs the code that pathfinds it to the “Milk” part, which will be changed later due to the fact that I will have multiple instances of things NPC’s can walk up to, and that will be chosen by getting a folder and doing "FolderChildrenVarible[math.random(1, #FolderChildren) Anyway, can you help?

I’m not quite sure how the position teleporting thing works, but if it’s the literal position, perhaps the npc glitched into it and flew out? Also is the root part anchored? I don’t remember exactly but I think it’s supposed to be not anchored? I’ll double check my resources.

Alright. Thanks man. I hope someone replies with a solution.

I’ve also noticed that it’s totally possible for the start point to be one store, but the end point to be a totally different store, because of the math.random part there. You can set a variable that controls this to ensure it’s the same store for start and end points.

Do you want the game file? I can send it to you and you can show me how that works? I do not understand what you mean?

Sorry I’m on mobile right now. I’ll get on my PC in about 5-15 minutes. I can explain much easier on my pc.

Alrighty! Great! I’ll send a game file!

So notice these 2 lines.

	local EndPosition = storesAval[math.random(1, #storesAval)].SpawnPart.Position
	local EndDestination = storesAval[math.random(1, #storesAval)].Milk

See the math.random part?
One of them could say 1, another could say 2. the position would be at store 1, but the end destination would be at store 2. to fix it you can just make a new variable for which store to go to, assuming that’s the table for all the stores.

local store = storesAval[math.random(1, #storesAval)]

Also, it would be a bit easier for me to help you with the game file so I can test things as well.

This might be because the gap between the walls obstructing the path is too small. Try making the walls non collidable.

What do you mean by this? Are you saying the NPC is glitching out of the map because of that?

Before I thought the video you showed was what you made sorry. As for the NPCs they shouldn’t be glitching out at all. I reviewed your code and

Humanoid.MoveToFinished:Wait(1)

the 1 doesn’t do anything as Wait() doesn’t take in any arguments.

Your script basically waits 5 seconds, spawns a npc, waits for the npc to get to the end position and then repeats in a while loop. If you do task.spawn(function() end) and put the for loop inside the task.spawn function then the script will instead wait 5 seconds, spawn a npc, repeat the while loop while also running the for loop code.

I can’t really help you with anything else as your script looks completely fine otherwise, hope this helped anyway.

Thanks for your help. Character limit

Question has not yet to be answered. I am still waiting. Anyone that wants to through their hat in the ring is welcome to do so!

If you are saying that they just do not spawn at the statring point, the issue is probably not with pathfinding, but with logic. I’d recommend trying to change the

Path:ComputeAsync(NPC.HumanoidRootPart.Position, EndDestination.Position)

to

Path:ComputeAsync(EndPosition.Position, EndDestination.Position)

And why do you define the PathfindingService and ReplicatedStorage inside the while loop?

yes i do know this is very old topic