:GetChildren() is returning weird values

Hello!

  1. What do you want to achieve? I’m doing on system, where I run for loop and do some other stuff with it, I use :GetChildren(), I expect it to return the Children of the Instance by order, from up, all the way down.

  2. What is the issue?

local RS = game:GetService("ReplicatedStorage")
local livesnum = 3
local currentnum = 0


RS.ShowLives.OnClientEvent:Connect(function(num)
	livesnum = num
	for i, LiveFrame in pairs(script.Parent.CurrentLives:GetChildren()) do
	if currentnum <= livesnum and LiveFrame:IsA("ImageLabel") then
		--LiveFrame.Visible = true
		print(LiveFrame.Name.." on "..i)
		currentnum = currentnum + 1
		end
	end
	currentnum = 0
end)

It prints:

025 on 1
001 on 2
002 on 3
003 on 4
004 on 5
005 on 6
006 on 7

This is the Hierarchy.
https://gyazo.com/a24b6fb20663129ea3b7cad7f1ea2f81

I expect it to print:
001 on 1
002 on 2
etc.

Every help is appreciated, also I tried removing the UIGridLayout, no change.

1 Like

Then, you would have to sort the children using table.sort. :GetChildren does not return in any specific order. A example of what you would do:

local children = script.Parent.CurrentLives:GetChildren()

table.sort(children, function(a, b)
    local an = tonumber(a)
    local bn = tonumber(b)
    if not an then return end
    if not bn then return end
    return an < bn --Edited From: return an > bn. See post #10. 
end)

for i, LiveFrame in pairs(children) do
	if currentnum <= livesnum and LiveFrame:IsA("ImageLabel") then
		--LiveFrame.Visible = true
		print(LiveFrame.Name.." on "..i)
		currentnum = currentnum + 1
	end
end

Explanation:
table.sort takes a table to be sorted and a function, which should return true if the first object should come before the first. If the object has a number, we sort it by that. If it does not, then we push it to the back of the table.

-Edit:
Please see :GetChildren() is returning weird values - #10 by ThatTimothy
for clarification.

3 Likes

Should I sort it only once, or every time I run the for loop?

Btw, thanks for your response. :slight_smile:

1 Like

I’m pretty sure one time is enough for that.

2 Likes

Sadly, it printed this:

025 on 1

016 on 2

017 on 3

015 on 4

014 on 5

013 on 6

EDIT: It prints the same numbers every time.
Note: I want to achieve this;
001 on 1
002 on 2
Etc

1 Like

You could like… This may sound dumb I guess. But like you remove the zeros from the names so you have just the number. Use a for loop to iterate through all the children. And then you put the children in a table depending on there number. So like youd do table.insert({children}, Child, tonumber(Child.Name))

This is basically the same thing as table.sort but you said table.sort wasnt working for you so I guess you could try something like this. Basically it would put child number 25 in the 25th position of the table, 24 and 24 and so on.

2 Likes

Thanks y’all for help. But I solved it, by doing for loop and doing it by name, by adding 00 or 0.

Whaa I just typed that XD right? Like 30 seconds before you replied. Thats crazy timing.

Yeah. :joy:
Thanks. :wink: But removing the 00 will disorder it. But thanks :).

2 Likes

Just wanted to add, you could still use the sort equation. I mistyped > instead of <. > sorts biggest to smallest, < sorts smallest to biggest. If you wanted to use the table.sort to sort from 1,2,3… ect., then you could use the following code:

local children = script.Parent.CurrentLives:GetChildren()

table.sort(children, function(a, b)
    local an = tonumber(a)
    local bn = tonumber(b)
    if not an then return end
    if not bn then return end
    return an < bn --Modified line, changed > to <
end)

for i, LiveFrame in pairs(children) do
	if currentnum <= livesnum and LiveFrame:IsA("ImageLabel") then
		--LiveFrame.Visible = true
		print(LiveFrame.Name.." on "..i)
		currentnum = currentnum + 1
	end
end

table.sort will put a in FRONT of b if TRUE is returned, and b in front of a if FALSE is returned.

Sorry for the confusion, hope this can help you sort the table in the future!

2 Likes