How to make children of model as a table to loop it thru top to bottom

im trying to make a map changer after each round in order A-Z without math.random so i tried it by indexing through the MapsToLoad folder but somehow it skips a child of the MapsToLoad so it will be like B-D-C-A not A-B-C-D

heres the code

local SS = game:GetService("ServerStorage")
local mtl = SS.CustomMaps:GetChildren() --mapstoload
local c = 1 -- this variable act as an index 
script.Parent.RegenControl.Signal.Changed:Connect(function()
	wait(5)
	local mtu = workspace.LoadedMaps:GetChildren() --mapstounload
	for _, m in pairs(mtu) do
		m:Destroy()
	end
	local mio = mtl[c]:Clone() --mapsinorder -- yeah you get what i mean
	mio.Parent = workspace.LoadedMaps	
	if c == #mtl then
		c = 1 -- jump back to the first child if it reached the folder length
	else
		c = c +1 -- otherwise add 1 to replace the first with 2nd-3rd etc etc
	end
end)

any help?
Because if I make a table of the folder that means I have to make a new line
to add another map in the MapsToLoad if I make a new map in the future so it will
take up alot of lines.

Hey, I just wrote up something that could help you.

This code should sort a table in alphabetical order (A - Z) from left to right. If it’s not what you’re looking for, let me know. I also wrote out of my own curiousity, as I like trying new things in programming. So, sorry if it’s horribly inefficient haha, I haven’t studied sorting algorithms quite yet lol.

Here it is:

local myTable = {"Hello", "Add", "Substract"}

local activated = true

while activated == true do
    for i = 1, #myTable, 1 do
        if myTable[i] and myTable[i - 1] then
            if string.byte(myTable[i]) >= 65 and string.byte(myTable[i]) <= 90 then
                if string.byte(myTable[i]) < string.byte(myTable[i - 1]) then
                    local previous = myTable[i - 1]
                    local current = myTable[i]

                    myTable[i] = previous
                    myTable[i - 1] = current
                    activated = true
                else
                    activated = false
                end
            end
        end
    end
end

EDIT: This will only work if,

  1. The elements in the array are strings
  2. The first character in each string is a letters A - Z or a - z (not symbols and such)
  3. Just a note, it isn’t bug proof or anything, so feel free to improve however you need

i tried using table.insert so this is my attempt

local mtl = SS.CustomMaps:GetChildren()
local q = {}
for _, i in pairs(mtl) do
table.insert(q,mtl) -- it errored here :/
end
local c = 1 

I dont mean by alphabetical order i wanted it to make the Children as a table so it goes from the first child to the last in order so how i do that?

What’s the error? I don’t believe you can insert anything into :GetChildren(), as I think it’s a read-only function.

So you want to put the :Children() into a seperate table? And what do you mean by first to last - numerically sorted, or what?

Folder:
. Koerer
.gegeogke
.jeojgoejgje
.ejgoejgoee
i want it to loop through the first child until the last (ejgoejgoee)

but when script ran, it started at the second to fourth to first and then third

Then just do something like this:

for i, v in pairs(SS.CustomMaps:GetChildren()) do
    -- Whatever you want to do
end

Is that not what you mean ?

1 Like

i changed it

local SS = game:GetService("ServerStorage")
local mtl = SS.CustomMaps:GetChildren() --mapstoload
local c = 1
script.Parent.RegenControl.Signal.Changed:Connect(function()
	wait(5)
	local mtu = workspace.LoadedMaps:GetChildren() --mapstounload
	for _, m in pairs(mtu) do
		m:Destroy()
	end
	for _, mapstoloop in pairs(mtl) do
		local mio = mapstoloop[c]:Clone()
		mio.Parent = workspace.LoadedMaps -- here, should it work?
	end
	if c == #mtl then
		c = 1
	else
		c = c +1
	end
end)

I believe that should work…

What is mio supposed to represent?

Welp, error again i seriously dont know what is going on :confused:

Send me the error message; what does it say?

1 is not a valid member of model
the mapstoloop[c] is the attempt to index through

1 Like

Does it specify a line in the error message?

And also, what’s the need for indexing the table with c, or using c at all?

From what I understand you’re trying to do this:

  1. Load a map into the game
  2. When a game is over (or whatever), remove that map from workspace.LoadedMaps, and instead put the next map into workspace.LoadedMaps

I feel like I have something wrong with the above - could you clarify what you’re trying to? Sorry for the hassle.

Because if I do a table its gonna be insufficient because I have to add another line when adding a new map
like

local MapsToLoad = {
RPA -- variable to point SS.CustomMaps.RPA
RPB
RPC
RPD
RPE 
-- next line has to be filled if wanting to add a new map
}

So i wanted to make the :GetChildren as a table to start indexing from the first to last model

Oh sorry, I meant on a wider scope. As in the big picture of what you’re trying to do - like with the maps step by step.

1 Like
local abc = "abcdefghijklmnopqrstuvwxyz"
local Index = 1


while true do
	local Found = false
	repeat
		if Index > 26 then Index = 1 end
		local char = abc:substr(Index, Index)
		for i,v in pairs(game.ServerStorage.CustomMaps:GetChildren()) do
			if v.Name:lower():substr(3,3) == char then
				-- Unload Old Map
				-- Load Map
				Found = true
			end
			Index += 1
			break
		end
	until
		Found = true
	wait(180)
end

You could try something like this…
I havent tested it or anything

1 Like