Script only inserting one variable in a nested table instead of multiple

  1. What do you want to achieve?
    I am trying to make a script that calculates how much of one item can be crafted (entirely for display purposes, as an actual script will deal with making sure and all that.)

  2. What is the issue?
    It’s only adding one of the “requiredMaterial” values as a table, here’s what it’s printing:

Actual Print
{
 [1] =  {
      ["Clay"] = 0
 },
 [2] =  {
      ["Tin Ore"] = 1
 }

Here’s what it should be printing:

Intended Print
{
 [1] =  {
      ["Clay"] = 0,
      ["Sand"] = 1
 },
 [2] =  {
      ["Tin Ore"] = 1
 }
  1. What solutions have you tried so far?
    I’ve tried changing all sorts of code and I’ve tried looking for things on the forums with no luck. I’m not sure where the problem is at this point. I think this might be important to mention, but I’m not sure, when I used table.insert instead of t[loopNumber] = {}, “Sand,” the missing value, was inserted, but again, it had more tables than it should of (3 instead of the current possible max of 2).
local t = {}
--there's code between all this, but i don't believe it's necessary to show it all
for loopNumber = 1,math.max(nums) do --the highest "nums" can be is 2
	for requiredMaterial,amount in pairs(array[loopNumber]) do --this gets a bunch of names and amounts to be inserted into the problematic table
		if loopNumber == nums then
			t[loopNumber] = {} --creates a table with the same name as the number of the loop, i tried a similar thing with table.insert but it created more tables than it's intended to
										
			local playerAmount = Inventory:FindFirstChild(requiredMaterial) --i believe this is pointless to put when it comes to asking for help for the problem, but i'll put it here just in case
											
			local craftableAmount = 0
			if playerAmount then
				craftableAmount = math.floor(playerAmount.Value / amount) --i believe this is pointless to put when it comes to asking for help for the problem, but i'll put it here just in case
			end
										
			t[loopNumber][requiredMaterial] = craftableAmount 
		end
	end
end
print(t)

I’m happy for any and all advice!

i think the issue is that you are creating the table, then adding the material and amount, and then for the next material is once again creating the table but since this time exist, it will override the past table.
What you should do is create the table above that loop like this:

for loopNumber = 1,math.max(nums) do --the highest "nums" can be is 2
t[loopNumber] = {}
	for requiredMaterial,amount in pairs(array[loopNumber]) do 
        --rest of the code
1 Like

oh my god

i’m never getting that time back over something dumb once again

1 Like