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

Why does my Pathfinding NPC not move Smoothly?

I'm trying to make an npc that follows the closest player. I have everything working (to my knowledge...) however the npc is a bit robotic in that it does not have smooth transitions between waypoints. I'm trying to figure out why that is. (i've ruled out lag since there are other npc's that move smoothly)

Here is a gif of whats happening https://gyazo.com/dbce5cbb8e78ab6a8b38c799dd77a68b

Since I'm not sure exactly what the problem is, here is the full script

local zombie = workspace.Zombie
local humanoid = zombie.Humanoid
local pathFindingService = game:GetService("PathfindingService")
local wayPointsIndex
local maxDistance = 200--detection range
local wayPoints = {}
local currentPoint

function findtarget()
	local closestDistance
	local closestPlayer
	for i,v in pairs(game.Players:GetPlayers())do
		if v.Character then
			local distance = (v.Character.HumanoidRootPart.Position-zombie.Torso.Position).magnitude
			if not closestDistance then closestDistance = distance end 
			if distance <= closestDistance and distance < maxDistance then
				closestDistance = distance
				closestPlayer = v
			end
		end
	end
	return closestPlayer
end

function createPath()
	local player = findtarget()
	if player then
		local character = player.Character
		local path = pathFindingService:CreatePath()
		path:ComputeAsync(zombie.Torso.Position,character.HumanoidRootPart.Position)
		local wayPoints = path:GetWaypoints()
		if path.Status == Enum.PathStatus.Success then
			for i,v in pairs(wayPoints)do
				if v.Action == Enum.PathWaypointAction.Jump then
					humanoid.Jump = true
				end
				humanoid:MoveTo(v.Position)
				humanoid.MoveToFinished:Wait(2)
			end
		end
	end
end


while true do
	createPath()
	wait()
end

Any help is appreciated :)

1 comment

0

I had the same problem. but I noticed in a server npc pathing movement is smoother compared to in the studio.

1 answer

I used to have the same problem too. I am not certain if this is the same problem as I encountered, but the way I solved about robot-like movement is by setting the network ownership of the npc to server(to nil), which will prevent from the server to automatically assign the client to calculate the physics of npc when they are close, which causes the npc to lag.

local function itterateModel(obj)
	for I,v in pairs(obj:GetChildren()) do
		if v:IsA("BasePart") then
			v:SetNetworkOwner(nil)
		end
		itterateModel(v)
	end
end

local npc = script.Parent

itterateModel(npc)

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