Is there a way to automate this process?

I have created a dictionary for each player that will be assigned a cell, is there a way to have a script create new dictionary entries from player1 all the way to player 14 on server startup? I have a folder containing all of the cells so I can use the length of the folder to generate the dictionary entries, if there’s a way to automate this process.

	CellContainer.player1 = {"Empty", "Cell1"} 
	CellContainer.player2 = {"Empty", "Cell1"} 
	CellContainer.player3 = {"Empty", "Cell1"} 
	CellContainer.player4 = {"Empty", "Cell1"} 
	CellContainer.player5 = {"Empty", "Cell1"} 
	CellContainer.player6 = {"Empty", "Cell1"} 
	CellContainer.player7 = {"Empty", "Cell1"} 
	CellContainer.player8 = {"Empty", "Cell1"} 
	CellContainer.player9 = {"Empty", "Cell1"} 
	CellContainer.player10 = {"Empty", "Cell1"} 
	CellContainer.player11 = {"Empty", "Cell1"} 
	CellContainer.player12 = {"Empty", "Cell1"} 
	CellContainer.player13 = {"Empty", "Cell1"} 
	CellContainer.player14 = {"Empty", "Cell1"} 

Create a for loop that runs over all existing players then use the index of the loop to get the specific player


This wasn’t what I had in mind, I know how I’d assign each “Empty” cell but what I want to do is automate the CellContainer.player1, CellContainer.player2, CellContainer.player3 etc so I don’t have to write them all down manually. I have 14 different entries written down in that script, I’m wondering if a loop can turn that into one entry being created 14 times.

There sure is. Remember for loops. We can say:

for a = 1, 10 do
    -- Do something

and whatever is in that loop is done 10 times, with a representing a different value for everything in the for loop each time.

Now, we can reference a table entry like this:
This is equivalent to TableName.EntryName. The entry name must be a string.

we can turn numbers into strings like this:
This transforms the number 1 into “1”, which is a string.

and we can concatenate (join) strings like this:
The result of which would be “hello”.

Therefore, when we put all of this together, your problem can be solved like this:

for PlayerNumber = 1, 14 do
    CellContainer["player"..tostring(PlayerNumber)] = {"Empty", "Cell1"}
1 Like