Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What type of loop will I need for this?

I am trying to shorten up my code, I am making a leaderstats which is replicating a table;

GameType = {"Balls", "FFA", "Swords"}

Using this I would like to create a list of the Maps with each GameType under it, so:

http://prntscr.com/avlosj

I have tried using a for loop

for (GameType) in pairs do
	local Stat = Instance.new("IntValue", StatsHolder)
end

Really not sure how to do it

2 answers

✓ Accepted answer

Generic for loop
Next, please.

for _,map in next, StatsHolder:GetChildren() do
	for __,type in next, GameType do
		local Stat = Instance.new("IntValue");
		Stat.Name = type;
		Stat.Parent = map;
	end;
end;

next is a function which returns the next key in a table. It takes a table and a key, where if the key is nil it returns the first entry in the table. If the key is not nil, it returns the next key,value pair in the table -after- that. It returns nil if there are no more keys.

pairs is a proxy for next. An iterator factory in essence.

You were right with the for loop idea. You just need to add a few lines.

for i, name in ipairs(GameType) do
	local val = Instance.new("IntValue", map)
	val.Name = name
end

So for each value in GameType, it will create an IntValue. The value's name will be whatever value in the table the loop is currently on. Define map as the map you want to put them in, obviously.

This discussion is preserved as it was. New answers, comments, and votes are closed.