Make Npc Pathfinding More smooth

I want to make my pathfinding more smooth

I’ve searched for around 2 days and couldn’t find anything that works with my script

local path = PathfindingService:CreatePath({
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = false,
			AgentCanClimb = false;
			Costs = {
				Water = 20;
				Snow = math.huge;
				Basalt = math.huge;
			}
		})
	
		

		
		local character = clone
		local humanoid = character:WaitForChild("Humanoid")

		

		local waypoints --= {workspace.TestF.Checker2, workspace.TestF.Checkpoint2, workspace.TestF.End2}
		local nextWaypointIndex
		local reachedConnection
		local blockedConnection

		local function followPath(destination, Goinside)
			-- 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()

				-- Detect if path becomes blocked
				blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
					-- Check if the obstacle is further down the path
					if blockedWaypointIndex >= nextWaypointIndex then
						-- Stop detecting path blockage until path is re-computed
						blockedConnection:Disconnect()
						-- Call function to re-compute new path
						followPath(destination)
					end
				end)
				
				-- Detect when movement to next waypoint is complete
				if not reachedConnection then
					reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
						if reached and nextWaypointIndex < #waypoints then
							-- Increase waypoint index and move to next waypoint
							nextWaypointIndex += 1
							humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
						else
							if Chosen == 2 then
								path = PathfindingService:CreatePath({
									AgentRadius = 2,
									AgentHeight = 5,
									AgentCanJump = false,
									AgentCanClimb = false;
									Costs = {
										Water = 20;
										Snow = math.huge;
										Basalt = 1
									}
								})
								workspace.Values.Bank.Value += 1
							elseif Chosen == 1 then
								clone:Destroy()

								local PlayersFriends2 = {}
								local plr
								for i, player in pairs(game.Players:GetChildren()) do
									plr = player
								end

								local success, page = pcall(function() return players:GetFriendsAsync(plr.UserId) end)
								if success then
									repeat
										local info = page:GetCurrentPage()
										for i, friendInfo in pairs(info) do
											table.insert(PlayersFriends2, friendInfo.Id)
										end
										if not page.IsFinished then 
											page:AdvanceToNextPageAsync()
										end
									until page.IsFinished
								end
								Npc.SpawmMultipleNpcs(PlayersFriends2, 1)
							end
							reachedConnection:Disconnect()
							blockedConnection:Disconnect()
						end
					end)
				end
				
				
				-- Initially move to second waypoint (first waypoint is path start; skip it)
				nextWaypointIndex = 2
				humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
			else
				warn("Path not computed!", errorMessage, clone)
				wait(.1)
				followPath(destination)
			end
		end
		
		local random = math.random(10, workspace.Values.ChanceOfSpawning.Value)
		
			
		if workspace.Values.Open.Value == true and random >= 11 then
			Chosen = 2
			followPath(workspace.TestF.inside1.Position)
		else
			if FinalSpawn == Spwn2 then
				Chosen = 1
				followPath(workspace.TestF.End2.Position)
			else
				
				followPath(workspace.TestF.End.Position)
			end
		end
1 Like

your issue might be the wait(0.1)

if its not the wait, i reccomend making your own tween mover instead of “MoveTo”

How would i replace MoveTo to Tween

I recommend this module. Makes pathfinding much simpler/faster and wayyy smoother.
I use it in all my games no issues.

But it is probably the wait. I would remove it and see what happens.

1 Like

Two things that might assist you with making a smoother system:

  1. Use RunService.Stepped
    Testing with RunService.Heartbeat, Stepped is the better option, it doesnt cause lag, and will make your pathfinding a lot faster than the standard loop.

  2. SetNetworkOwner
    I would Recommend setting NetworkOwnership to the Server by putting nil within the Parameter, this will make it smoother in most cases as it NetworkOwnership is switching between Players:

HumanoidRootPart:SetNetworkOwner(nil) -- Server is the Owner

I use these 2 things for my Pathfinding and they work well.

I would Also Recommend CollectionService for your code to run for multiple Instances.

6 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.