Why does it say Im attempting to index a nil while Im not?

Soo I have a script that manages the bots pathfinding and other stuff. I defined the humanoidrootpart but for some reason, in a function it says that my rootpart is trying to index nil with “Position”. I dont know why this is happening

this is basically where the error occurs:

local function Pathfind(destination)
    local path = Pathfinding:CreatePath(PathInfo)

    path:ComputeAsync(RootPart.Position, destination.Position)

    return path
end

Rootpart is the humanoidrootpart of the npc and the destination is basically the positions the npc wants to go to which are the players and waypoints

here’s the function that uses the actual pathfinding:

local function Wandering(Location)
    local path = Pathfind(Location)
    if path.Status == Enum.PathStatus.Success then
        for i,Waypoint in pairs(path:GetWaypoints()) do
            if iswalking == false then
                local Target = TargetPlayer()
                local Player = game:GetService("Players"):GetPlayerFromCharacter(Target)
                if Target and Target.Humanoid.Health > 0 and Player:FindFirstChild("Safe") and Player.Safe:IsA("BoolValue") and Player.Safe.Value == false then
                    Attack(Target)
                    break
                elseif walkingToDoor==false then
                    RootPart.CanCollide = true
                    movingPart = Location
                    Hum.WalkSpeed = normSpeed
                    AnimWalk()
                    value.Value = false
                    if iswalking == false then
                        Hum:MoveTo(Waypoint.Position)
                    end
                    Hum.Died:Connect(function()
                        respawned()
                    end)
                    local moveTime = Hum.MoveToFinished:Wait()
                    if not moveTime then
                        IsStuck(true)
                    else
                        IsStuck(false)
                    end
                end
            end
        end
    else
        local DoorDetect = FindDoor()
        if DoorDetect~=nil then
            walkingToDoor = true
            Hum:MoveTo(DoorDetect.Position + RootPart.CFrame.LookVector * 2)
            Hum.MoveToFinished:Wait()
            walkingToDoor = false
            Wandering(Location)
        else
            walkingToDoor = true
            Hum:MoveTo(Location.Position)
            RootPart.CanCollide = false
            Hum.MoveToFinished:Wait()
            walkingToDoor = false
        end
    end
end
local function RandomPartWalk()
    local waypointsParts = PathwaysParts:GetChildren()
    local RandomNum = math.random(1, #waypointsParts)
    Wandering(waypointsParts[RandomNum])
end

the AI is a rig model

image

the issue seems strange because it works on freshly new games but not on my main game.

I tried some stuff out but with no success. I tried to change the rootpart and destination position to the cframes position but of course it gives me an error with the cframe attempting to index nil with position. Then I tried to make an if-statement which just check if the destination or rootpart is nil and if it is it should return nil.

if RootPart == nil or destination == nil then
        return nil
    end

    path:ComputeAsync(RootPart.Position, destination.Position)

also with no success since I got an error in the wandering function which said Im trying to index nil with ‘Status’. So I made a check if the status is nil and if it is it should basically return.

if path == nil then
        return
    end

this basically fixed all the errors, made the npc work but really broken. it barely walks. Does anyone have an idea of fixing it?

Have you checked the value of destination inside Pathfind?
Without knowing the exact data types, I can’t tell for sure but it looks like Pathfind expects a BasePart but gets an actual position.

2 Likes

Nope I didnt. Should I print the type of the destination?

Oh wait I just checked the type of the destination and rootpart and the rootpart gave me ‘userdata’ and the destination ‘nil’

Uhm Guys I fixed it I guess. I forgot to name the parts with numbers
image

I basically had a function for the loopoints and I forgot that the function finds the child by their numbers

1 Like

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