How to get children of an Instance if they have to clone yet?

I wanna get the children of an Instance (a Leaderboard) that will clone frames with player names on it and every method that I tried failed because I wouldn’t get the cloned children.

Code:


local SurfaceGui = Leaderboard.SurfaceGui

local Contents_Frame = SurfaceGui.Frame.Contents

local ItemsFrame = Contents_Frame.Items

for i, PlayerNameFrame in pairs(ItemsFrame:GetChildren()) do
	task.wait(10)
	if PlayerNameFrame:IsA("Frame") then
		print(PlayerNameFrame.Name)
	end
end

Here’s a screen shot of the instance that I wanna get the children of.

Screenshot 2022-06-17 114243

1 Like

My take on this would be, that you call :GetChildren() before the children are even created. Try yielding until the children are finished creating and then loop through them.

Never heard of yielding and never used it before could you please give me a link a dev forum post. I’m new to lua and have to yet learn some stuff, but thank you for your suggestion!

1 Like

Yielding basically means stopping the script from running temporarily.
So you could wait until the children are finished creating and then you call :GetChildren() to loop through them.

Another way to do this would be firing an event once the children are finished creating. You could then listen to that event to be fired.

1 Like

Try doing :ChildAdded .
This will fire each time anything is added to the Items folder.

local SurfaceGui = Leaderboard.SurfaceGui

local Contents_Frame = SurfaceGui.Frame.Contents

local ItemsFrame = Contents_Frame.Items

local Connection

function OnPlayerFrameAdded(PlayerFrame)
if PlayerFrame:IsA("Frame") then
print(PlayerFrame.Name.." Was Added to the Items Frame!")
Connection:Disconnect()
end
end

Connection = ItemsFrame.ChildAdded:Connect(OnPlayerFrameAdded)

1 Like

If you’re expecting an instance to have children added to it then you should hook a callback function to its ‘ChildAdded’ event. Here’s an example which uses the ‘Workspace’ container.

local Workspace = workspace

local function OnWorkspaceChildAdded(Child)
	print(Child.Name)
end

Workspace.ChildAdded:Connect(OnWorkspaceChildAdded)

local Part = Instance.new("Part")
Part.Name = "Test"
Part.Parent = Workspace
1 Like

Exactly. But there is a problem. If Exploiters add childs to the Leaderboard the code will nonetheless execute.you should use something known CollectionService to Validate those frames.

This is only an issue if the script listening for children to be added is a local script.

In the screenshot posted, the script is a server script.

I have found a method of my own, it’s kind of dumb. But here it is, so I disabled the script and used another script to enable it back after 10 seconds, but thank you for taking your time to write this response. I will try this code soon and inform you if it works or not.

Oh sorry didnt notice :stuck_out_tongue: (charrsss)

Its well not dumb but if a new player joins at the same time the script will be disabled. Either enable it when player joins or u could try other methods.

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