I’m trying to make a little NPC roaming mechanic where a character randomly chooses from a folder with parts placed in intended positions, and path-finds to them. However, the character keeps pausing for a little bit upon reaching a waypoint.
local me = script.Parent
local event = game.ReplicatedStorage["bindable events"]["npc system"].roam
local efinish = me.Event
local destbank = game.Workspace["dest system"]
local service = game:GetService("PathfindingService")
event.Event:Connect(function(finish, systemname, char)
if finish == false then
if char == me.Parent.Name then
local value = false
local starter = false
local system = destbank:FindFirstChild(systemname)
local destinations = 0
for _, part in pairs(system:GetChildren()) do
if part:IsA("BasePart") then
destinations = destinations + 1
end
end
while value == false do
task.wait()
local hum = me.Parent:FindFirstChildWhichIsA("Humanoid")
local randomdest = math.random(1, destinations)
local found = system:FindFirstChild(tostring(randomdest))
local goal = found.Position
if hum then
local hrp = hum.RootPart
local path = service:CreatePath({
AgentRadius = me.Parent:GetAttribute("radius"),
AgentHeight = me.Parent:GetAttribute("height"),
AgentCanJump = me.Parent:GetAttribute("canjump"),
AgentCanClimb = me.Parent:GetAttribute("canclimb"),
WaypointSpacing = 5,
Costs = {
danger = math.huge
}
})
path:ComputeAsync(hum.RootPart.Position, goal)
local points = path:GetWaypoints()
for _, p in ipairs(points) do
hum:MoveTo(p.Position)
hum.MoveToFinished:Wait()
end
task.wait(math.random(me.Parent:GetAttribute("minWaitRange"), me.Parent:GetAttribute("maxWaitRange")))
end
event.Event:Connect(function(finish)
if finish == true then
value = true
end
end)
end
end
end
end)
Here’s a little video presentation