#trouble making an npc that chases you via pathfindingservice

1 messages · Page 1 of 1 (latest)

fathom pilot
#

been trying to make a chasing pathfinding thing..
its just supposed to chase any player who's near but the issue is with the MoveToFinished:Wait()

it has to finish the path to actually redirect to player again and this makes it really easy to just. outrun/juke them
but if i remove the wait then the pathfinding just doesnt work at all since it instantly moves to last waypoint

can anyone help me make it pathfind properly while not having to wait to walk to a waypoint?

local pfs = game:GetService("PathfindingService")
local rs = game:GetService("RunService")
local plrs = game:GetService("Players")

local npc = script.Parent
local hum = npc.NPCHumanoid

local path
local wp
local range = 50

local function CreatePath(trgt:Player)
    local plrpath = pfs:CreatePath({
        AgentCanClimb = true,
        AgentCanJump = true,
        AgentRadius = 3
    })
    local trgthrp = trgt.Character:FindFirstChild("HumanoidRootPart")

    local success, errormsg = pcall(function()
        plrpath:ComputeAsync(npc.HumanoidRootPart.Position, trgthrp.Position)
    end)

    if success and plrpath.Status == Enum.PathStatus.Success then
        return plrpath
    end
end

local function GetPlayerInRange()
    local mags = {}
    local trgt

    for _,plr in pairs(plrs:GetPlayers()) do
        local dist = plr:DistanceFromCharacter(npc.HumanoidRootPart.Position)

        if dist <= range then
            table.insert(mags, {dist, plr})
        end
    end

    table.sort(mags, function(a,b)
        return a[1] < b[1]
    end)

    if #mags > 0 then
        trgt = mags[1][2]
    end

    return trgt
end

while rs.Heartbeat:Wait() do
    local trgt:Player = GetPlayerInRange()

    if trgt then
        repeat task.wait() until trgt.Character or trgt.CharacterAdded:Wait()
        path = CreatePath(trgt)

        if path then
            wp = path:GetWaypoints()

            for _,wyp in pairs(wp) do

                if game:GetService("RunService"):IsStudio() then
                    local visual = Instance.new("Part", workspace)
                    visual.Anchored = true
                    visual.CanCollide = false
                    visual.Position = wyp.Position
                    visual.Size = Vector3.one
                    visual.Color = Color3.fromRGB(255,0,0)
                    visual.Material = Enum.Material.Neon

                    visual.TopSurface = Enum.SurfaceType.Smooth
                    visual.BottomSurface = Enum.SurfaceType.Smooth

                    game:GetService("Debris"):AddItem(visual, .2)
                end

                hum:MoveTo(wyp.Position)

                if wyp.Action == Enum.PathWaypointAction.Jump then
                    hum.Jump = true
                end
                
                hum.MoveToFinished:Wait()
            end

        else
            hum:MoveTo(npc.HumanoidRootPart.Position)
            path = nil
            wp = nil
        end
    end
end
#

Watch with wait by nexusity and millions of other Roblox Studio videos on Medal. Tags: #robloxstudio

▶ Play video

Watch without wait by nexusity and millions of other Roblox Studio videos on Medal. Tags: #robloxstudio

▶ Play video
tropic moat
#

Im trying to find this out right now too

indigo harbor
fathom pilot
indigo harbor
#

notice there is no movetofinished:wait(). that's your first clue.

fathom pilot
#

Hi okay yeah so i fixed it i had to rework my script entirely because im abysmal at coding

fathom pilot