`WaitForChild` Issue

I’m currently working on an enemy system for this game I’m working on, and to make sure everything inside of the enemy model is loaded, it runs a loop through the model using WaitForChild. For some reason, items keep returning nil despite clearly existing on both ends. This is the code I’m using:


CollectionService:GetInstanceAddedSignal("NPC"):Connect(function(AIModel)

    for _, object in ipairs(AIModel:GetDescendants()) do

        print(object.Name);

        AIModel:WaitForChild(object.Name);

    end

    if AIModel:IsA("Model") then

        local NPC = AIHandler.new(AIModel);

        NPC:RunNPC();

    end

end)

Here are some screenshots showing what I’m talking about:
image
image

Any help would be appreciated :slight_smile:

Since the loop uses :GetDescendats(), any child that is parented under the child of AIModel will cause that. Switch that to :GetChildren()

1 Like
CollectionService:GetInstanceAddedSignal("NPC"):Connect(function(AIModel)

    for _, object in ipairs(AIModel:GetDescendants()) do

        print(object.Name);

        --AIModel:WaitForChild(object.Name);
        --What's the purpose of it. Maybe child's child was added too...

    end

    if AIModel:IsA("Model") then

        local NPC = AIHandler.new(AIModel);

        NPC:RunNPC();

    end

end)

I actually tried that before, but then it would still return nil for important items within the model.

1 Like

might try doing

AIModel:WaitForChild(object.Name,10)

to prevent the annoying infinite yield warning

Tried this, still returning objects as nil.

Then, what if that important model was privated or 404’d or backdoor within.

Tried this, it takes way too long to load in all the objects for some strange reason.