Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My NPC's pathfinding script is not working, so how do I fix the problem?

Asked by 3 years ago

I made a script for an NPC where it walks from one point to another in a while loop, but if it sees a player it goes after them. I tried moving some things around to see if it works. I also get an error saying "Workspace.Guard.Script:28: attempt to index nil with 'Parent'". I need help finding out what is wrong in the script.

Script:

local firstpoint = workspace.pointforguard.Position
local secondpoint = workspace.pointforguard1.Position
local hum = script.Parent:WaitForChild("Humanoid")
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
local model = script.Parent
local player = game.Players.LocalPlayer
local hum = script.Parent:WaitForChild("Humanoid")
local t = script.Parent:WaitForChild("UpperTorso")
local pfs = game:GetService("PathfindingService")


model.PrimaryPart = hrp

while true do
    wait(2)
        hum:MoveTo(firstpoint)
            hum.MoveToFinished:Wait()
                model:SetPrimaryPartCFrame(hrp.CFrame * CFrame.Angles(0,math.rad(180), 0))
                    wait(15)
            hum:MoveTo(secondpoint)
        hum.MoveToFinished:Wait()
    wait(15)
    function getTarget()
        target = nil
        for i, v in pairs(game.Workspace:GetChildren()) do
            human = v:FindFirstChild("Humanoid")
            torso = v:FindFirstChild("LowerTorso")
            if human.Parent and torso.Parent == player.Character then
                target = torso
                break
            end
        end
    end
    if target == torso then
        while wait() do
            local torso = getTarget()
            if torso then
                local path = pfs:CreatePath()
                path:ComputeASync()
                local waypoint = path:GetWaypoints()
                for i, waypoint in pairs(waypoint) do
                    human:MoveTo(torso.Position)
                    human.MoveToFinished:Wait(2)
                end
            end
        end
    else
        return target
    end
end
0
you should make it a server script otherwise its movements are going to be different for every player in the game Robowon1 323 — 3y
0
It is a server script and also the game is only going to have 1 player per server KPNagai 4 — 3y
0
The problem that I know of right now is with the error I typed at the top. I am not sure why it is calling the variables human and torso "nil" KPNagai 4 — 3y

1 answer

Log in to vote
0
Answered by
megukoo 877 Moderation Voter
3 years ago

The LowerTorso has a potential to be nil (player died). You need to account for that case by first checking if the part even exists.

if torso then
    if human.Parent and torso.Parent == player.Character then
        target = torso
        break
    end
end

It is also worth mentioning :Wait() takes no arguments (the 2 does nothing there.) MoveToFinished will end when the engine has determined the goal was reached, or timeout after 8 seconds if the humanoid seems stuck.

0
Ok I will change that and remove the 2. I honestly thought the argument could be changed to wait however long you want. KPNagai 4 — 3y
Ad

Answer this question