Hey guys.
I have a function in my NPC which handles all my pathfinding and movement. But there’s a few problems with it.
First of all my NPCs are lagging when they move (Stuttering). I’ve set the network ownership of all the parts to Nil but its still happening. and to my knowledge the function only runs if its not already running. EDIT (Also the NPC moves incredibly slow. much slower than its walk speed. and its walking animation stutters like crazy)
Second of all. The path sometimes doesn’t compute even if the path is literal straight line. It has some doorways to walk through and objects to go around but definitely enough space to compute a path. Also there is no invisible walls, but there are parts that aren’t collidable. But I’m pretty certain the pathfinding algorithm ignores them anyway. So not sure why this happens. I’ve had to add teleporting if NPC cannot make a path
And Thirdly, a lot of the time when the path does compute. The NPC does get stuck on things. so I had to add a timer that if its not moved in 5 seconds just teleport it to the destination. The NPC will just walk into a wall and keep trying to move through it like its not there.
Extra Infomation:
The NPCs are 4.5 Studs tall and 3 studs wide (So shorter than an average ROBLOX character)
The door frame is 6 Studs tall and 5 studs wide (Should be more than enough room right?)
CODE:
local PathfindingService = game:GetService("PathfindingService")
local NPC = script.Parent
function Travel(Position)
--Create Path
local PathParams = {AgentRadius = 1, AgentHeight = 3, AgentCanJump = false ,WaypointSpacing = 2}
local Path = PathfindingService:CreatePath(PathParams)
local success, errorMessage = pcall(function()
Path:ComputeAsync(NPC.HumanoidRootPart.Position, Position)
end)
local WayPoints = Path:GetWaypoints()
--MoveToPoint
for i = 1,#WayPoints do
local WaypointPosition = WayPoints[i].Position
local Count = 0
NPC.Humanoid:MoveTo(WaypointPosition)
repeat
local distance = (WaypointPosition - NPC:WaitForChild("HumanoidRootPart").Position).magnitude
wait(.1)
Count += 0.1
until distance <= 5 or Count >= 5
if Count >= 5 then -- If NPC has not reached the next waypoint in 5 seconds teleport to destination
NPC:SetPrimaryPartCFrame(CFrame.new(Position)+ Vector3.new(0,2.5,0))
break
end
end
NPC.Humanoid:MoveTo(Position)
if Path.Status == Enum.PathStatus.NoPath then -- If a path cant compute, Teleport to destination
NPC:SetPrimaryPartCFrame(CFrame.new(Position)+ Vector3.new(0,2.5,0))
elseif Path.Status ~= Enum.PathStatus.NoPath then
warn("Successful Travel")
end
end
I’m not asking for a whole new function. I’m just trying to work out what’s wrong with this one. Any help or ideas would be greatly appreciated
Thanks ~ Anon