GetDescendants() not getting the descendants?

I feel like I’m losing my mind.

I’m making a tycoon game. I have this module script that has a build function. It gets a specific instance (in this case, the Boxes folder). I use the GetDescendants() method to get all the instances I need to make visible.

It works for everything except these boxes. I’ve tried to get the descendants with the same method in a separate script and it works fine, but for some reason it doesn’t work in the function. I even threw in a GetChildren for fun and it only returns the Button instance. Below I’ve put the script and the folder. The output shows the instance from the build script, the output from the GetDescendants, and the output from the GetChildren.


4 Likes

Is the instance argument for BuildInstance possibly pointing to a template that doesn’t have these crates?

1 Like

No, it gets the folder through another script that directly references it.

1 Like

Make sure the crates and Racks have Archivable set to true. When testing the game in Studio, Archivable=false Instances will vanish and won’t appear at runtime.

1 Like

They’re all Archivable. I turned em on and back on to make sure there was no mistake. Could this be just a studio bug?

I don’t know why Studio would do this, but it’s a possibility. You’ll want to do something other than just printing the descendants if you publish and play this on the website since it’ll just give you a table address in an actual game server. Printing each descendants in a for loop should work fine.

No yeah, I was just using the print statement so that I could see what the GetDescendants was returning. I’m using this function to “build” parts in a tycoon game. I just change the transparency to 0 and turn CanCollide on. The parts in this specific folder aren’t being built though because GetDescendants isn’t recognizing them for some reason.

In the BuildInstance function, consider using the ChildAdded event on the instance, then add a print statement within that function to see if it detects any of the crates being replicated after the fact.

Alternatively / additionally, consider temporarily swapping out the “Boxes” folder with a Model that has its ModelStreamingMode set to Atomic (if you have StreamingEnabled turned on). That would make sure that all the descendants of the “Boxes” container are streamed in at the same time, which means that by the time you call :GetDescendants() on it, it should be guaranteed to return all of its descendants.


However, since it appears that you’re calling the module from a server script and the Wood Crates already exist in the Workspace before the game even starts, it should already be replicated… so if neither of these possible solutions end up working, it’ll probably be tough to really figure out what’s causing the issue to happen without knowing more about the other script which is directly referencing the “Boxes” folder and sending it through to the module.

Good suggestions, unfortunately neither of them seemed to do anything.
Here’s the other script. It just references the folder in the table. The rest is just a function for when the player steps on the button.

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local levelUpEvent = ReplicatedStorage.LevelUp

local buildScript = require(ServerScriptService.BuildScripts.BuildScript)

local banks = workspace.Banks:GetChildren()
for _, bank in banks do
	local owner = bank.Owner
	
	local ceiling = bank.SecondFloor.Ceiling
	local roofAccessFolder = bank.RoofAccess
	local ATMFolder = bank.ATMs
	local facadeFolder = bank.Facade
	local lightsFolder = bank.Lights
	local breakRoom = bank.BreakRoom
	
	local buttons = {ATMFolder.ATM1.Button, ATMFolder.ATM2.Button, ATMFolder.ATM3.Button, ATMFolder.ATM4.Button,
		ceiling.Ceiling1.Button, ceiling.Ceiling2.Button, ceiling.Ceiling3.Button, ceiling.Ceiling4.Button, ceiling.Ceiling5.Button,
		roofAccessFolder.DoorWall.Button, roofAccessFolder.Floor.Button, roofAccessFolder.Wall1.Button, roofAccessFolder.Wall2.Button,
		breakRoom.Door.Button, roofAccessFolder.Wall3.Button, roofAccessFolder.Stairs.Button, roofAccessFolder.Door.Button, 
		breakRoom.Lockers.Button, breakRoom.Storage.Button, breakRoom.BreakRoomStuff.Button, breakRoom.VendingMachines.Button, 
		facadeFolder.Pillars.Pillar1.Button, facadeFolder.Pillars.Pillar2.Button, facadeFolder.Pillars.Pillar3.Button, 
		facadeFolder.Pillars.Pillar4.Button, facadeFolder.Pillars.Pillar5.Button, facadeFolder.Pillars.Pillar6.Button, 
		facadeFolder.Pillars.Pillar7.Button, facadeFolder.Pillars.Pillar8.Button, facadeFolder.Front.Button, 
		lightsFolder.Chandelier.Button
	}
	
	for index, button in buttons do
		local COST = button.Cost.Value
		button.Touched:Connect(function(hit)
			if not button:GetAttribute("Touched") then
				button:SetAttribute("Touched", true)

				if buildScript.instanceTouched(COST, hit, owner.Value, button.Parent, bank) then
					button:Destroy()

					local character = Players:GetPlayerFromCharacter(hit.Parent)

					--Activate the next button if there is one
					if #buttons > index then
						buildScript.activateButton(buttons[index+1])
					else
						character.leaderstats.Money.Value += 3000
						print("Bank Level 5 Completed. Awarded $3000")
						levelUpEvent:Fire(bank, 8)
					end
				end

				wait(0.1)
				button:SetAttribute("Touched", false)
			end
		end)
	end
end

It connects to this other part in the main module script. Which then takes it, runs it through a function to make sure the player has enough money to buy it, then runs it through the BuildInstance function.

function BuildScript.instanceTouched(COST, partTouched, ownerName, instance, bank)
	if CheckPrice(COST, partTouched, ownerName, instance) then
		BuildInstance(instance, bank)
			
		return true
	end

	return false
end

I got it boys. The parts had CanCollide off and were completely transparent because I needed to build them in-game right? Well I should have thought to check if they were still there in-game because they weren’t anchored. Can’t believe I made such a rookie mistake and wasted 3 hours trying to figure it out. Such a seemingly complex issue had such a simple solution. Always remember the systems not faulty, you are. Check the basics.

4 Likes

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