Roblox :GetChildren() isnt returning instances

  1. Ive been trying to create a for loop to automate proximityprompts for drawers for a horror game

  2. The issue is that when I use Getchildren() it doesn’t return instances

  3. I’ve tried adding wait(5) and checking if there are more then 1 children in the table and etc

wait(5)

-- >> Services
local ReplicatedStorage = game:GetService('ReplicatedStorage');
local TweenService = game:GetService('TweenService');

-- >> Variables
local Cupboards = game:GetService('Workspace'):WaitForChild('Cupboards');

-- >> Functions

-- Testing

local function PlaySound(Sound: Sound, Part: BasePart)
	if (Part) then
		Sound:Clone()
		Sound.PlayOnRemove = true;
		Sound.RollOffMaxDistance = 100;
		Sound.Volume = 1;
		Sound.Parent = Part
	end
	
	Sound:Destroy();
end

local function SetupCupboards()
	if Cupboards then -- Passes this
		print('Initiating Cupboards'); -- Prints this 
	end

	for i,v in pairs(Cupboards:GetChildren()) do
		print(i,v); -- Doesnt print this at all please help

		if v.Name == 'Cupboard' then
			local CupboardHolder = v

			print('There is a cupboard holder');

			for _, Cupboard in pairs(CupboardHolder:GetChildren()) do
				if Cupboard:GetAttribute('Setup') then return end
				
				Cupboard:SetAttribute('Setup', true)
				Cupboard:SetAttribute('State', 'Closed');
				Cupboard.ActionText = 'Open';

				Cupboard.ProximityPrompt.Triggered:Connect(function()
					local CupboardState = Cupboard:GetAttribute('State')
					if CupboardState == 'Closed' then
						PlaySound(Cupboard:WaitForChild('Open'), Cupboard);
						Cupboard.ActionText = 'Close';
					elseif CupboardState == 'Opened' then
						PlaySound(Cupboard:WaitForChild('Close'), Cupboard)
						Cupboard.ActionText = 'Open';
					end
				end)
				
			end
		end;

	end
end


repeat task.wait() until #Cupboards:GetChildren() >= 1 -- It passes this part
print("Number of Cupboards:", #Cupboards:GetChildren()) -- It prints 3
print(Cupboards:GetChildren()) -- This prints nothing at all

SetupCupboards()

Also I’ve turned off streaming enabled and streaming enabled doesn’t seem to be the issue

4 Likes

Can we see the Cupboards children in the workspace property

2 Likes

image

2 Likes

Try running the game and checking if the cupboards still exist in the explorer. They may be destroyed if they happen to be unanchored.

2 Likes

I’ve setup an example in a workspace just like you had it. It doesn’t print in the console, but it prints in the output.

4 Likes

I’ve checked and it still exists, all anchored and no destroyed.

2 Likes

THANK YOU SO MUCH i will now be using output now often.

2 Likes

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