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

I can't loop through this table?

I have five tables (slots). For some reason, I can't loop through all fives tables and change their values.

Table:

CurrentFluff = {

	CurrentFluff1 = { --Sets Current Fluff Variables 
		BodyType = (""),
		BodyColor = (""),
		FluffDescription = (""),		
		FluffName = (""),
		FluffGender = ("")				
						},
	
	CurrentFluff2 = { --Sets Current Fluff Variables 
		BodyType = (""),
		BodyColor = (""),
		FluffDescription = (""),		
		FluffName = (""),
		FluffGender = ("")				
						},
	
	CurrentFluff3 = { --Sets Current Fluff Variables 
		BodyType = (""),
		BodyColor = (""),
		FluffDescription = (""),		
		FluffName = (""),
		FluffGender = ("")				
						},
	
	CurrentFluff4 = { --Sets Current Fluff Variables 
		BodyType = (""),
		BodyColor = (""),
		FluffDescription = (""),		
		FluffName = (""),
		FluffGender = ("")				
						},
	
	CurrentFluff5 = { --Sets Current Fluff Variables 
		BodyType = (""),
		BodyColor = (""),
		FluffDescription = (""),		
		FluffName = (""),
		FluffGender = ("")				
						}
				}			

For some reason, I can't loop through the tables, so I have to resort to this disgusting mess. This mess below still works.

	if CurrentFluff.CurrentFluff1.FluffName == "" then
		CurrentFluff.CurrentFluff1.BodyType = NewFluff.BodyType
		CurrentFluff.CurrentFluff1.BodyColor = NewFluff.BodyColor
		CurrentFluff.CurrentFluff1.FluffDescription = NewFluff.FluffDescription
		CurrentFluff.CurrentFluff1.FluffName = NewFluff.FluffName
		CurrentFluff.CurrentFluff1.FluffGender = NewFluff.FluffGender
		
	elseif CurrentFluff.CurrentFluff2.FluffName == "" then
		CurrentFluff.CurrentFluff2.BodyType = NewFluff.BodyType
		CurrentFluff.CurrentFluff2.BodyColor = NewFluff.BodyColor
		CurrentFluff.CurrentFluff2.FluffDescription = NewFluff.FluffDescription
		CurrentFluff.CurrentFluff2.FluffName = NewFluff.FluffName
		CurrentFluff.CurrentFluff2.FluffGender = NewFluff.FluffGender
		
	elseif CurrentFluff.CurrentFluff3.FluffName == "" then
		CurrentFluff.CurrentFluff3.BodyType = NewFluff.BodyType
		CurrentFluff.CurrentFluff3.BodyColor = NewFluff.BodyColor
		CurrentFluff.CurrentFluff3.FluffDescription = NewFluff.FluffDescription
		CurrentFluff.CurrentFluff3.FluffName = NewFluff.FluffName
		CurrentFluff.CurrentFluff3.FluffGender = NewFluff.FluffGender
		
	elseif CurrentFluff.CurrentFluff4.FluffName == "" then
		CurrentFluff.CurrentFluff4.BodyType = NewFluff.BodyType
		CurrentFluff.CurrentFluff4.BodyColor = NewFluff.BodyColor
		CurrentFluff.CurrentFluff4.FluffDescription = NewFluff.FluffDescription
		CurrentFluff.CurrentFluff4.FluffName = NewFluff.FluffName
		CurrentFluff.CurrentFluff4.FluffGender = NewFluff.FluffGender
		
	elseif CurrentFluff.CurrentFluff5.FluffName == "" then
		CurrentFluff.CurrentFluff5.BodyType = NewFluff.BodyType
		CurrentFluff.CurrentFluff5.BodyColor = NewFluff.BodyColor
		CurrentFluff.CurrentFluff5.FluffDescription = NewFluff.FluffDescription
		CurrentFluff.CurrentFluff5.FluffName = NewFluff.FluffName
		CurrentFluff.CurrentFluff5.FluffGender = NewFluff.FluffGender
	end

2 answers

✓ Accepted answer
for i,maintable in pairs(CurrentFluff) do
      for a,newtable in pairs(maintable) do
	     if newtable.FluffName == "" then
                 newtable.BodyType = NewFluff.BodyType
    		 newtable.BodyColor = NewFluff.BodyColor
 	         newtable.FluffDescription = NewFluff.FluffDescription
                 newtable.FluffName = NewFluff.FluffName
                newtable.FluffGender = NewFluff.FluffGender
             end
      end
end

1 comment

0

Thanks, I thought I had to use ipairs.

Explanation

You can iterate through a table via in pairs which uses the parameter of the table via each index and value in the table. Our first iteration will give you all of the tables CurrentFluff#.

Next we iterate through these table (matching the value of our first iteration), where your new key and value are defined in your code.

Since each of the keys in the tables match the key you are checking in NewFluff, you can simply use the key index of the second table to affect all values.

Iteration

for _, fluff in pairs(CurrentFluff) do
	for constant, value in pairs(fluff) do
		if fluff["FluffName"] == "" then
			fluff[constant] = NewFluff[constant]
		end
	end
end

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