Module Help, not printing things inside of table

Working on a shop in studio, and while trying to mess with a module to update some values, I’m experiencing something weird. When I run through the script, “print(“WHERE IS MY PRINT?”)” does not print. Why? I’m confused. MOO and MOO END! prints though.

Script here

local ShopInfo = require(script.ShopInfo)

print("MOO")

local WeaponStash = ShopInfo.NoobWeapons
for p = 1, #WeaponStash do
	print("WHERE IS MY PRINT?")
	local Category = WeaponStash[p]
	for d = 1, #Category do
		local CurrentWeapon =Category[d]
		if game.ServerStorage.Weapons:FindFirstChild(CurrentWeapon) then
			CurrentWeapon.ThumbnailImage = game.ServerStorage.Weapons:FindFirstChild(CurrentWeapon.TextureId)
		end
	end
end

print("MOO END!")

Module Config Below

local ShopData = {
	NoobWeapons = {
		MeleeWeapons = {
			["Sword"] = {
				Cost = 100,
				WaveToUnlock = 1,
				Desc = "The Sword is a cheap lethal weapon: it's capable of dealing harmful damage to five targets at once, and grants the user the ability to perform a Spin Attack! Grants an additional five walkspeed boost while equipped.",
				ThumbnailImage = "",
			},
		},
	},
}

return ShopData

You’re using a numeric for loop on a dictionary, which doesn’t work because #list is 0.

Use for _, Category in pairs(WeaponStach) do etc instead.

4 Likes

Ah! Thank you!

A table in Lua contains an array part and a dictionary part. You are only using the dictionary part which is where your error lies. # can be used to get the number of elements within the array part of the table and as you have 0 items in the array part of this table you will not run the loop.

You can use the code below or any other method for accessing the dictionary part of the table.

for i, v in pairs(ShopInfo.NoobWeapons) do
      print(i, v)
end

I would also reccomend that you change the structure of this code to allow for better maintainability. After all this list could get rather large.

local ShopData = {}

local function newGroup(name)
	if ShopData[name] then error("group name exists") end

	local tmp = {}
	ShopData[name] = tmp
	
	local f = {}
	
	return function(name) -- add new group function
		if tmp[name] then error("weapon group exists") end
		
		local tmp2 = {}
		tmp[name] = tmp2
		
		return function(name, cost, unlockWave, desc, icon) -- add item function
			if tmp2[name] then error("Item exists in weapon group") end
			
			tmp2[name] = {
				Cost = cost,
				WaveToUnlock  = unlockWave,
				Desc = desc,
				ThumbnailImage  = icon
			}
		end
	end
end


local noobWeaponsGroup = newGroup("NoobWeapons")
local addMeleeWeapon = noobWeaponsGroup("MeleeWeapons")

addMeleeWeapon("Sword", 100, 1, "Desc", "icon")

-- code from my table print module
local function printTable(tbl)
	if type(tbl) ~= 'table' then return nil end
	local depthCount = -15

	local function run(val, inPrefix)
		depthCount = depthCount + 15
		if inPrefix then print(string.rep(' ', depthCount) .. '{') end	
		for i,v in pairs(val) do
			if type(v) == 'table' then
				print(string.rep(' ', depthCount) .. ' [' .. tostring(i) .. '] = {')
				run(v, false)
			else
				print(string.rep(' ', depthCount) .. ' [' .. tostring(i) .. '] = ' .. tostring(v))
			end
		end
		
		
		print(string.rep(' ', depthCount) .. '}')
		depthCount = depthCount - 15
	end
	
	run(tbl, true)
end

printTable(ShopData)

hope this helps