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

Setting Up Table For NPC Spawning?

Asked by
0Vyp 14
4 years ago
Edited by Ziffixture 4 years ago

So I am trying to make an area-specific NPC spawning. I have different NPCs set up for different areas.

This is my Area setup

And this is the NPC set up for each area. (The NPCs are in the specific folders)

--// Variables

local replicatedStorage = game:GetService("ReplicatedStorage")
local AreasFolder = game:GetService("Workspace"):WaitForChild("Areas")
local AreaNPCsFolder = replicatedStorage:WaitForChild("AreaNPCs")

local AreaNPCs = {}

for _, Area in pairs(AreasFolder:GetChildren()) do
    table.insert(AreaNPCs, Area.Name)

    local NPCs = AreaNPCsFolder:FindFirstChild(Area):GetChildren()
    table.insert(AreaNPCs[Area], NPCs.Name)
end

Basically I want the tabled to be formated as shown:

local AreaNPCs = {
    ["Spawn"] = {NPC Names Go Here},
    ["Lava Hot Springs"] = {NPC Names Go Here},
    ["Nuclear Wasteland"] = {NPC Names Go Here},
    ["VIP"] = {NPC Names Go Here}
}

I am quite unsure of what's wrong with this so anything helps.

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The :GetChildren() method of Instance returns an array first-descendant objects. As you do with creating dictionary references for each "area", you must iterate through each individual NPC and append their names respectively:

--###----------[[SERVICES]]----------###--
local ReplicatedStorage = game:GetService("ReplicatedStorage")



--###----------[[VARIABLES]]----------###--
local NPCSpawnAreas = workspace.Areas

local KnownAreaNPCs = ReplicatedStorage.AreaNPCs


local AreaNPCArray = {}



--###----------[[LOGIC]]----------###--
for _, Area in pairs(NPCSpawnAreas:GetChildren()) do
    print("Traversing: \""..Area.Name.."\"")
    ---------------
    local AreaNPCs = KnownAreaNPCs:FindFirstChild(Area.Name)
    if (AreaNPCs) then
        ---------------
        AreaNPCArray[Area.Name] = {} 
        --// Initialize the dictionary reference/array.
        ---------------
        print("Allocated dictionary index \""..Area.Name.."\"\n")
        ---------------
        for _, NPC in pairs(AreaNPCs:GetChildren()) do
            ---------------
            print("Added NPC name \""..NPC.Name.."\" to dictionary index \""..Area.Name.."\"")
            ---------------
            table.insert(AreaNPCArray[Area.Name], NPC.Name)
        end
        ---------------
        print("\n")
    end
end

EDIT:

I can assure you that the program is performing the requested task. I recreated the conditions of your game with line-debugging to visually prove this:

Asset outlines can be found here: Workspace folders, ReplicatedStorage folders.

Output confirmation can be found here.

Additionally, I've also adjusted the code above with the necessary print statements.

0
Could I get in contact with you on discord? I've sent you a friend request my discord is 0Vyp#9314 0Vyp 14 — 4y
Ad
Log in to vote
0
Answered by
0Vyp 14
4 years ago
Edited 4 years ago

I actually just got it working.

--// Variables

local replicatedStorage = game:GetService("ReplicatedStorage")
local AreasFolder = game:GetService("Workspace"):WaitForChild("Areas"
local AreaNPCsFolder = replicatedStorage:WaitForChild("AreaNPCs")

local AreaNPCs = {}

--// Main
for _, Area in pairs(AreaNPCsFolder:GetChildren()) do
    table.insert(AreaNPCs, Area)

    local SelectedArea = Area
    local NPCs = Area:GetChildren()
    table.insert(AreaNPCs, NPCs)
end

Thanks for anyone who took a look at this post. I was just trying stuff out and got it working.

0
The primary issues with your code, was that you first created an index via the Area name, then tried to reference through the Area Instance. NPCs is an array of the Area's children, meaning .Name wouldn't operate in the context you desired. Ziffixture 6913 — 4y
0
I have written you a program below that can appropriately construct the array you asked for. Ziffixture 6913 — 4y
0
My mistake I figured out what I was doing wrong, Thanks for the help! 0Vyp 14 — 4y

Answer this question