Killer NPC doesn't walk

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want fix Killer NPC, he can’t walk
  2. What is the issue? Include screenshots / videos if possible!
    He can’t walk.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
local killer = script.Parent
local humanoid = killer.Humanoid

local attacked = false

local ps = game:GetService("PathfindingService")
killer.PrimaryPart:SetNetworkOwner(nil)

local function findTarget()
    local players = game.Players:GetPlayers()
    local maxdist = 40
    local nearestTarget

    for _, player in pairs(players) do
        if player.Character then
            local target = player.Character
            local distance = (killer.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

            if distance < maxdist then
                nearestTarget = target
                maxdist = distance
            end
        end
    end

    return nearestTarget
end

local function getPath(destination)
    local path = ps:CreatePath()
    path:ComputeAsync(killer.HumanoidRootPart.Position, destination.Position)
    return path
end

local function attack(target)
    local distance = (killer.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

    if distance > 2.5 then
        humanoid:MoveTo(target.HumanoidRootPart.Position)
    else
        if not attacked then
            attacked = true
            target.Humanoid:TakeDamage(50)
            wait(0.25)
            attacked = false 
        end
    end
end

local function walkTo(destination)
    local path = getPath(destination)

    if path.Status == Enum.PathStatus.Complete then -- Check if the path is valid
        for _, waypoint in ipairs(path:GetWaypoints()) do
            humanoid:MoveTo(waypoint.Position)
            local result = humanoid.MoveToFinished:Wait() -- Wait until the movement is finished

            if result then -- Check if the movement was successful
                local target = findTarget() -- Check for a target after reaching each waypoint
                if target then
                    print("Target found: " .. target.Name)
                    attack(target)
                    return -- Exit walkTo after attacking to handle the target
                end
            else
                print("Movement to waypoint failed, stopping patrol.")
                break
            end
        end
    else
        print("Pathfinding failed, cannot reach destination.")
    end
end

local function patrol()
    local waypoints = workspace.Waypoints:GetChildren()
    if #waypoints > 0 then
        local randomnum = math.random(1, #waypoints)
        walkTo(waypoints[randomnum])
    end
end

while wait() do
    patrol()
end

1 Like

You wanna do target.HumanoidRootPart.Position - killer.HumanoidRootPart.Position instead, since the format is GoToPos - StartingPos

PS: Just remove the result thing since its unnecessary, seeing as the script will yield until the killer finishes moving

2 Likes

Uhh… Not working tbh… What to do?

are there any errors? if so list them

I fixed it, thanks who helped me

Make sure you mark the thread as ‘solved’.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.