Questions about "arrays in tables"

I’m trying to make a furniture system, so I can store all the names of my furniture/buildings, along with the level and price so I can use it later on.

Will it function correctly and efficiently like this example below?

local Furniture = {
	--// Name, UpgradeLvl, Price
	{"Fire Station", 1, 1},
	{"Police Station", 2, 1}
}

I can get it to print only the first array. For example:

for _, furni in pairs(Furniture) do
	print(furni)
	return furni[1], furni[3], FurniAmount
end

It’s probably something super obvious, but I’m quite new to making a system like this, and any help would be extremely appreciated. Thanks! :slight_smile:

1 Like

The question here is pretty vague, but I’ll try my best to answer this. On a side note, tables and arrays are the same thing, and arrays with arrays inside are called nested arrays.

Your script isn’t working because using the return function stops the function. This will result in the next iteration never taking place. A good way to demostrate this is through simplified instructions:

--Start a loop that runs through all values in Furniture
--Start the first iteration. 
--Use the print method using the selected value. 
--Return some values inside our selected values. Stop the currently running loop. 

As you can see, a second iteration never occurs as the return method stops the current scope.

Keep in mind that you can indeed simply use the values inside of the loop, with no need for a function.

for _, furni in pairs(Furniture) do
	local FurniName = furni[1]
    local FurniPrice = furni[3]

    -- Use variables here! 

end

If you’re still confused as to how return works, here’s a another post here on the forum which describes it quite well: What does 'return' do? - #3

1 Like

You can do even better:

local Furniture = {
	{Name = "Fire Station", UpgradeLvl = 1, Price = 1},
	{Name = "Police Station", UpgradeLvl = 2, Price = 1},
}

for _, furni in pairs(Furniture) do
	print(furni.Name, furni.UpgradeLvl, furni.Price)
end
5 Likes

This makes it lot easier, thanks. :slight_smile:

The script & the table is in a ModuleScript. If I wanted to access the properties of the array (e.g. If I wanted to get the price of the Fire Station), could I do that any other way other than using return exampleblah?

2 Likes

Why don’t you simply return the entire Furniture array? It can be looped through and used in the script that required it.

1 Like

The reason why it’s only getting the first array is because you use return. Return makes the loop exit out of the loop scope, thus terminating it. You ought to use a search function that gets the table you need.

On another hand, you can speed up your table lookup slightly by using ipairs instead of pairs since you don’t have any indices (thus implied numerical) in your table. A dictionary is probably also easier to work with as far as the actual table content goes (what Kacper posted).

1 Like

The table is much easier to work with if you sort it this way:

local Furniture = {
	Fire_Station = {UpgradeLvl = 1, Price = 1},
	Police_Station = {UpgradeLvl = 2, Price = 1},
}
--Get the Fire Stations's price:
return Furniture.Fire_Station.Price

If you wanted to loop through with a ipairs() loop, the index will be the name, and the value the array. With this method, you don’t have to search the entire array for one value.

Return ends the current scope and returns the values to the call of the function. Of you wanted to return multiple sets, put them into a array, then return them.

1 Like

Yeah, I just fixed it. Realized after I posted it that it was in a different format.

1 Like

Okay, I did some work based on the responses.

	local Furniture = Module
	print(#Furniture)
	
	for _, furni in ipairs(Furniture) do
		for i = 0, #Furniture do
			local ItemToClone = script.Parent.ItemBackground.ScrFrame.BuildItems.Item
			local Clone = ItemToClone:Clone()
			Clone.Parent = script.Parent.ItemBackground.ScrFrame.BuildItems
			Clone.Price.Text = furni.Price
		end
	end
end

^ This is in a LocalScript as it handles the Build GUI. However, it doesn’t clone (due to the table being seen as empty). After doing a bit of research, I found (quite obviously) that the # operator only works if there isn’t a string as the index, but that’s only afaik, it could be outdated.

In the ModuleScript, the dictionary now looks like this:

local Furniture = {
	--// Name, UpgradeLvl, Price
	Fire_Station = {UpgradeLvl = 1, Price = 1},
	Police_Station = {UpgradeLvl = 1, Price = 2},
}

Aand, I returned the table at the end of the Module.
return Furniture

Is there any way I can get the length of the dictionary?
(also, with the old dictionary and function, it seemed to make too many clones (as seen in the LocalScript above ^^ but now it’s making none, due to the reason above)

2 Likes

Well if the # operator doesn’t work you could try:

local size = 0

for _, something in pairs(Furniture) do
    size = size + 1
end
--size is now the size of the dictionary
1 Like

I was thinking something like this, wasn’t sure if it was the best way though. Thanks :slight_smile:

I still seem to have the issue with it repeating.

	for _, entry in pairs(Module) do
		tSize = tSize + 1
		print(tSize)
	end
	
	for _, furni in pairs(Furniture) do
		for i = 0, tSize do
			print(i, tSize)
			local ItemToClone = script.Parent.ItemBackground.ScrFrame.BuildItems.Item
			local Clone = ItemToClone:Clone()
			Clone.Parent = script.Parent.ItemBackground.ScrFrame.BuildItems
			Clone.Price.Text = furni.Price
		end
	end

The i in the for i = 0, tSize do prints:

0 1 2 0 1 2 (obviously repeating itself twice, but I’m not sure why. Could be to do with the things in the dictionary because 2 buildings with 6 different values in total which are name, lvl & price)

For each furni are you trying to repeat it by how many indexes + values they’re?

1 Like

The script I compiled is the one listed in my previous post ^

The dictionary looks like this:

local Furniture = {
	--// Name, UpgradeLvl, Price
	Fire_Station = {UpgradeLvl = 1, Price = 1},
	Police_Station = {UpgradeLvl = 1, Price = 2},
}

So afaik, it should only be counting the indexes. The same problem happened with using #Furniture (when it was applicable with the old array)

1 Like

Oh so you’re trying to do:

			print(i, tSize)
			local ItemToClone = script.Parent.ItemBackground.ScrFrame.BuildItems.Item
			local Clone = ItemToClone:Clone()
			Clone.Parent = script.Parent.ItemBackground.ScrFrame.BuildItems
			Clone.Price.Text = furni.Price

Two times? (Amount of indexes)
Sorry, I’m kinda confused right now.

1 Like

Your thread is starting to get slightly obscure. As far as I know, your original question pertained to a multi-dimensional array only iterating through one entry and the efficiency of your code. It seems to have branched off into code errors?

It’d be worth starting a new topic or asking a general question about your code, so all of those issues can be addressed quickly and altogether. I’m not too sure what you’re trying to accomplish right now, so it’s hard to provide fixes as well.

2 Likes

Alright, will do. I’ll mark the original answer as complete. Thanks for the contributions. :slight_smile:

2 Likes