So, i want to make it so that my pathfinding ai doesnt stutter everytime it moves to a waypoint. The issue is that it does stutter and i cant find a way to fix it. I saw solutions on the dev forum, but some of them werent working with the code i had (I didnt know how to implement it) and also they werent using the same npc movement system i did
This is my code
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local radius = 100
local function getRandomPosition(center, radius)
local angle = math.rad(math.random(0, 360))
local offsetX = math.sin(angle) * radius
local offsetZ = math.cos(angle) * radius
return center + Vector3.new(offsetX, 0, offsetZ)
end
local function moveRandomly()
local npc = script.Parent
local center = npc.PrimaryPart.Position
local randomPosition = getRandomPosition(center, radius)
local path = PathfindingService:CreatePath()
path:ComputeAsync(npc.PrimaryPart.Position, randomPosition)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
npc.Humanoid:MoveTo(waypoint.Position)
npc.Humanoid.MoveToFinished:Wait()
end
end
local function startMovingRandomly()
while true do
moveRandomly()
wait(1)
end
end
wait(1)
startMovingRandomly()
the reason it’s stuttering is because after it pathfinds to the spot its waiting 1.
while true do
moveRandomly()
task.wait() --you still need a wait to prevent the loop from getting 'exhausted' also task.wait is better for the game
end
i accidentally marked ur stuff as solution even tho idk yet but
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local agentParams = {
AgentRadius = 100,
}
local function getRandomPosition(center, AgentRadius)
local angle = math.rad(math.random(0, 360))
local offsetX = math.sin(angle) * agentParams.AgentRadius
local offsetZ = math.cos(angle) * agentParams.AgentRadius
return center + Vector3.new(offsetX, 0, offsetZ)
end
local function moveRandomly()
local npc = script.Parent
local center = npc.PrimaryPart.Position
local randomPosition = getRandomPosition(center, agentParams.AgentRadius)
local path = PathfindingService:CreatePath(agentParams)
path:ComputeAsync(npc.PrimaryPart.Position, randomPosition)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
npc.Humanoid:MoveTo(waypoint.Position)
npc.Humanoid.MoveToFinished:Wait()
end
end
local function startMovingRandomly()
while true do
moveRandomly()
task.wait()
end
end
wait(1)
startMovingRandomly()
is this how its meant to be now? if not can u tell me where to fix if you know