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.
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
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.
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.
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!