How would I get the name and value of variables in a module script?

I’m making a shop system for my game where each tool will cost different ingredients in different quantities, I have stored all of these in a Module Script just as shown in the example below :

["Fork"] = {
		["Iron"] = 5,
		["Stone"] = 15,
		["Raspberry"] = 10,
	},

Now, to show each ingredient required, I’m trying to get the variables inside “Fork” such as “Raspberry” and the amount needed, but I am not acquainted with Module Scripts so I’m not sure how I would gather the variables & their values like a :GetChildren() loop but for variables.

Doing it manually was proven to be very unefficient as there are over 10 ingredients so far with more to come in the future and some tools don’t require all of the ingredients.

This might be an easy fix but I just can’t think of anything right now!

You could prolly use table.unpack or smthn if that fits your situation.

P.S. table.unpack returns a list of all items in the provided table (parameter 1)

Not sure if I’m not doing it right or something but it just returns an error : “invalid argument #1 to ‘pairs’ (table expected, got nil)”

local array = table.unpack(weaponModule[class])
print(array)

The print is just there to check but it returns nil, I’m sure weaponModule[class] works because I’m using it for the description of the item and it works just fine.

did your require the module or just location to the module, please show us more.

I did require yes. Here’s a bit more data you might need

local weaponModule = require(game.ReplicatedStorage:WaitForChild("ShopEvents"):WaitForChild("ShopWeapons")) 

script.Parent.MainFrame.RightArrow.MouseButton1Click:Connect(function()
for i,v in pairs(camobj:GetChildren()) do --Stuff done above the code, works fine
			if v.Name == "Class" then
				
				local class = v.Value
				
				script.Parent.MainFrame.Cost.ObjectName.Text = class --Actually says the correct name of the class	
				
				script.Parent.MainFrame.Cost.Description.Text = weaponModule[class].Description --Works perfectly fine

				local array = table.unpack(weaponModule[class])
				print(array) --Returns nil
			end
		end
end)

The module is set up just as mentioned before.

Can’t you just like do for name, cost in pairs(weaponsModule[class]["Costs") do

1 Like

or you can do like iron,stone,raspberry = table.unpack(weaponsModule[class])

This worked like a charm, didn’t know doing this was possible, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.