Roblox Studio | How to make a NPC Pathfinding System

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ธ.ค. 2024

ความคิดเห็น • 8

  • @tropicalmasterpiece
    @tropicalmasterpiece  16 วันที่ผ่านมา

    NPCMover Script -
    local pathService = game:GetService("PathfindingService")
    local NPC = script.NPC.Value
    local humanoid = NPC.Humanoid
    local npcPaths = workspace:FindFirstChild("NPCPaths")
    local npcPoints = npcPaths:FindFirstChild("NPCPoints")
    local spawnpoint, endpoint
    -- Generate random spawn and end point
    repeat
    spawnpoint = npcPoints:GetChildren()[math.random(1,#npcPoints:GetChildren())]
    endpoint = npcPoints:GetChildren()[math.random(1,#npcPoints:GetChildren())]

    task.wait()
    until spawnpoint ~= endpoint
    -- Move NPC to spawn
    NPC:PivotTo(spawnpoint.CFrame)
    -- Animations
    local idleAnim = Instance.new("Animation",humanoid)
    idleAnim.AnimationId = "rbxassetid://507766388"
    idleAnim = humanoid.Animator:LoadAnimation(idleAnim)
    local walkAnim = Instance.new("Animation",humanoid)
    walkAnim.AnimationId = "rbxassetid://507767714"
    walkAnim = humanoid.Animator:LoadAnimation(walkAnim)
    -- Roblox Documentation - PathfindingService
    local Waypoints, NextWaypoint, Reached, Blocked
    -- Determine what the NPC will and wont walk on
    local mCosts = {
    -- Prioritize Smaller Numbers
    Concrete = 1,

    Plastic = 1000,
    Grass = 1000,
    SmoothPlastic = 1000
    }
    -- Create path
    local path = pathService:CreatePath({
    AgentRadius = 3,
    AgentHeight = 5,
    AgentCanJump = false,
    Costs = mCosts
    })
    local function moveNPC(endPoint)
    local success, errorMessage = pcall(function()
    path:ComputeAsync(NPC.HumanoidRootPart.Position, endPoint.Position)
    end)

    -- If the path succeeds
    if success and path.Status == Enum.PathStatus.Success then
    Waypoints = path:GetWaypoints()
    Blocked = path.Blocked:Connect(function(blockedWaypointIndex)
    if blockedWaypointIndex >= NextWaypoint then
    Blocked:Disconnect()
    moveNPC(endPoint)
    end
    end)
    if not Reached then -- If the NPC still needs to move
    Reached = humanoid.MoveToFinished:Connect(function(reached)
    if reached and NextWaypoint < #Waypoints then
    NextWaypoint += 1
    humanoid:MoveTo(Waypoints[NextWaypoint].Position)
    if walkAnim.IsPlaying ~= true then
    idleAnim:Stop()
    walkAnim:Play()
    end
    else
    walkAnim:Stop()
    idleAnim:Play()

    Reached:Disconnect()
    Blocked:Disconnect()

    task.wait(1)

    NPC:Destroy()
    script:Destroy()
    end
    end)
    end
    NextWaypoint = 2
    humanoid:MoveTo(Waypoints[NextWaypoint].Position)
    else
    warn(errorMessage)

    NPC:Destroy()
    script:Destroy()
    end
    end
    moveNPC(endpoint)

  • @DrakenTheOg
    @DrakenTheOg 24 วันที่ผ่านมา +1

    great video man! keep going :D

  • @thatqne
    @thatqne 22 วันที่ผ่านมา

    ServerScriptService.NPC Handler:139: attempt to compare nil = nextwaypoint then
    blocked:Disconnect()
    moveNPC(endpoint)
    end
    I also get a warn error message of nil

    • @tropicalmasterpiece
      @tropicalmasterpiece  22 วันที่ผ่านมา

      Could you provide me with your entire moveNPC local function?

    • @thatqne
      @thatqne 22 วันที่ผ่านมา +1

      @@tropicalmasterpiece it was because the nextwaypoint variable was nil when trying to compare it

  • @thatqne
    @thatqne 22 วันที่ผ่านมา

    is there a way to pathfind on certain parts with names instead of materials?

    • @tropicalmasterpiece
      @tropicalmasterpiece  22 วันที่ผ่านมา +1

      I'm not sure, probably not
      But you can also use invisible parts with a concrete material if you want the NPC to follow specific paths.

    • @thatqne
      @thatqne 22 วันที่ผ่านมา

      @@tropicalmasterpiece OHH I FOUND THE ISSUE. the parts were invisible, but need cancollide to properly work as a pathfinding material/part