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)
01 | --// Variables |
02 |
03 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 | local AreasFolder = game:GetService( "Workspace" ):WaitForChild( "Areas" ) |
05 | local AreaNPCsFolder = replicatedStorage:WaitForChild( "AreaNPCs" ) |
06 |
07 | local AreaNPCs = { } |
08 |
09 | for _, Area in pairs (AreasFolder:GetChildren()) do |
10 | table.insert(AreaNPCs, Area.Name) |
11 |
12 | local NPCs = AreaNPCsFolder:FindFirstChild(Area):GetChildren() |
13 | table.insert(AreaNPCs [ Area ] , NPCs.Name) |
14 | end |
Basically I want the tabled to be formated as shown:
1 | local AreaNPCs = { |
2 | [ "Spawn" ] = { NPC Names Go Here } , |
3 | [ "Lava Hot Springs" ] = { NPC Names Go Here } , |
4 | [ "Nuclear Wasteland" ] = { NPC Names Go Here } , |
5 | [ "VIP" ] = { NPC Names Go Here } |
6 | } |
I am quite unsure of what's wrong with this so anything helps.
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:
01 | --###----------[[SERVICES]]----------###-- |
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 |
04 |
05 |
06 | --###----------[[VARIABLES]]----------###-- |
07 | local NPCSpawnAreas = workspace.Areas |
08 |
09 | local KnownAreaNPCs = ReplicatedStorage.AreaNPCs |
10 |
11 |
12 | local AreaNPCArray = { } |
13 |
14 |
15 |
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.
I actually just got it working.
01 | --// Variables |
02 |
03 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 | local AreasFolder = game:GetService( "Workspace" ):WaitForChild( "Areas" |
05 | local AreaNPCsFolder = replicatedStorage:WaitForChild( "AreaNPCs" ) |
06 |
07 | local AreaNPCs = { } |
08 |
09 | --// Main |
10 | for _, Area in pairs (AreaNPCsFolder:GetChildren()) do |
11 | table.insert(AreaNPCs, Area) |
12 |
13 | local SelectedArea = Area |
14 | local NPCs = Area:GetChildren() |
15 | table.insert(AreaNPCs, NPCs) |
16 | end |
Thanks for anyone who took a look at this post. I was just trying stuff out and got it working.