Help with pathfinding? (Npc stopping/taking a while to move)

Hello!

So I might have ran into an issue with my pathfinding, you see, I made a game (a joke game) with my friend.

Well, to make the npc’s move, I had to use pathfinding, and when the npc goes to me, it sometimes stops and takes a while to move to me again.

robloxapp-20220428-0755092.wmv (3.4 MB)

robloxapp-20220428-0756008.wmv (2.8 MB)

I also realized that the other little ones

aren’t moving, now I could fix this by just making a script for everyone of them, but is it possible to make it so they all pathfind using one script?

Edit: He also gets stuck, as I just found out. So three problems now; stopping, stuck and some not pathfinding.

image

Thanks!!

For this issue, you can just put the bots in a folder, then call a :GetChildren function, after that just treat it as if it were a single NPC.

Also can you provide your current script?

1 Like

I did just that actually, but im pretty sure it doesn’t work due to it waiting till one pathfinding is done.

sure!

local Players = game:GetService("Players")
local PathFindingService = game:GetService("PathfindingService")
local Visuals = {}
local function ViewWayPoints(WP) -- pass the waypoints
	for n, x in pairs(Visuals) do
		x:Destroy()
	end
	local Att = Instance.new("Attachment")
	local Bem = Instance.new("Beam")
	Bem.FaceCamera = true
	Att.Visible = true
	for n, x in pairs(WP) do
		local Point = Att:Clone()
		Point.Parent = workspace.Terrain
		Point.Position = x.Position
		if n ~= 1 then
			local Con = Bem:Clone()
			Con.Attachment1 = Point
			Con.Attachment0 = Visuals[n-1]
			Con.Parent = Point
		end
		Visuals[n] = Point
	end
end

Players.PlayerAdded:Connect(function(plr)
	local Lord = game:GetService("Workspace"):WaitForChild("OurLordAndSavior")
	local humanoid = Lord.Humanoid
	for _, v in pairs(Lord:GetChildren()) do
		if v:IsA("BasePart") then
			v.CanCollide = false
			
			v.Touched:Connect(function(hit)
				if hit:IsA("BasePart") then
					hit.Anchored = false
				elseif Players:GetPlayerFromCharacter(hit.Parent) then
					hit.Parent.Humanoid.Health = 0
				end
			end)
			
		end
	end
	
	local function FindPlayer()
		local NearestTarget
		local AttackTarget
		local AttackDist = 29
		local maxDist = math.huge

		for i, player in  pairs(Players:GetPlayers()) do
			if player.Character then
				local target = player.Character
				local Distance = (Lord.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

				if Distance < maxDist then
					NearestTarget = target
					maxDist = Distance
					AttackTarget = NearestTarget
					AttackDist = Distance
					return NearestTarget, AttackTarget
				end
			end
		end
	end
	
	local function ToPath()

		local NearestTarget, AttackTarget = FindPlayer()
		local pathParams = {
			["AgentRadius"] = 37.935,
			["AgentHeight"] = 71.996,
			["AgentCanJump"] = true		
		}

		local path = PathFindingService:CreatePath(pathParams)

		if NearestTarget then
			path:ComputeAsync(Lord.HumanoidRootPart.Position, NearestTarget.HumanoidRootPart.Position)
		end
		
		if path.Status == Enum.PathStatus.Success then
			if AttackTarget then

				local Waypoints = path:GetWaypoints()

				for i, waypoint in pairs(Waypoints) do
					ViewWayPoints(Waypoints)
					humanoid:MoveTo(waypoint.Position)
					humanoid.MoveToFinished:Wait(2)
				end
			else
				local Waypoints = path:GetWaypoints()

				for i, waypoint in pairs(Waypoints) do
					ViewWayPoints(Waypoints)
					humanoid:MoveTo(waypoint.Position)
					humanoid.MoveToFinished:Wait(2)
				end
			end
			end
		end

	while true do
		wait()
		ToPath()
	end
end)

Note: this is for the big boi, same script with the little one anyway

I believe coroutines will solve this issue. However I’m not too familiar with them… therefore I don’t know how to help you in using it in your script, just know the facts :sweat_smile:

1 Like

Ah, thank you! " A coroutine is used to perform multiple tasks at the same time from within the same script." Im guessing the coroutine is used for the

how about the other two problems? since I’m stuck on them the most :sweat_smile:, I appreciate the big help though!

Looking back at the edit, you can use a blocked event to prevent it from getting stuck. Annoyingly as always, you’ll have to look for this event inside the path finding tutorial on the dev hub, there is a section about it!

1 Like

Wow thanks! I never thought of that!! :smiley:
I actually found a solution for the lagigng/pausing thing:

for the stuck thing:

and for others not moving:

1 Like