โ† Back to all scripts

NPC Wander (Pathfinding)

NPC that randomly wanders using PathfindingService with jump support.

NPCIntermediateScript inside NPC Model
npc-wander-pathfinding.lua
-- NPC Wander with PathfindingService
local PathfindingService = game:GetService("PathfindingService")
local npc      = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local root     = npc:WaitForChild("HumanoidRootPart")
local RADIUS   = 30

local function walkTo(target)
    local path = PathfindingService:CreatePath({ AgentRadius=2, AgentHeight=5 })
    local ok = pcall(function() path:ComputeAsync(root.Position, target) end)
    if not ok or path.Status ~= Enum.PathStatus.Success then return end
    for _, wp in ipairs(path:GetWaypoints()) do
        if wp.Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end
        humanoid:MoveTo(wp.Position)
        humanoid.MoveToFinished:Wait()
    end
end

while task.wait(3) do
    if humanoid.Health > 0 then
        local p = root.Position
        walkTo(Vector3.new(
            p.X + math.random(-RADIUS, RADIUS), p.Y,
            p.Z + math.random(-RADIUS, RADIUS)
        ))
    end
end
Related scripts in NPC
NPC Typewriter DialogueIntermediate โ†’
Something broken or not working?
๐Ÿ”ง Try the AI Script Fixer โ†’