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?
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
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?
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).
--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.
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:
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)
I was thinking something like this, wasn’t sure if it was the best way though. Thanks
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)
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.