# gives 0 but its not

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    get the actually #table printed
  2. What is the issue? Include screenshots / videos if possible!
    it prints 0
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    adding more to the table

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--script
local mm = require(script.Parent.MainModule)
local wood = Instance.new("NumberValue",game.Players:WaitForChild('Qin2007'))
wood.Value = 1
wood.Name = 'wood'
wood.Changed:Connect(print)
local recipeResult=mm.new('Instant',{In = {wood = 50,stone=70},Out={Value=80,Name='wood'},Name='Qin2007sFirst'})
print(recipeResult:Craft(game.Players:WaitForChild('Qin2007')))
--module script
local module = {}
function module.getRecipe(recipe)
	local recipeResult = {ingredients=recipe.In}
	recipeResult.Out = recipe.Out
	recipeResult['Name'] = recipe.Name
	--
	
	function recipeResult:Craft(player)
		if #self.ingredients>0 then
			local payd = true
			for k,v in pairs(self.ingredients) do
				--print(k,v)
				local ingredient = player:WaitForChild(k,7)
				if ingredient then
					ingredient.Value -= v.Value
					if ingredient.Value < 0 then
						ingredient += v.Value
						print('he cant pay')
						return false, 'he cant pay'
						
					end
				else
					return false,'cant find'
				end
				
			end
			local out = player:FindFirstChild(self.Out.Name)
			if out then
				out.Value += self.Out.Value
				return true, Enum.ProductPurchaseDecision.PurchaseGranted
			else
				--refund
				for k,v in pairs(self.ingredients) do
					local ingredient = player:FindFirstChild(k)
					ingredient += v.Value
				end
				return false,Enum.ProductPurchaseDecision.NotProcessedYet
			end
		else
			print(self.ingredients,#self.ingredients,typeof(self.ingredients))
		end
		error('whaaaaaaaa')
	end
	return recipeResult
end

return module

mainModuleScript
--mainModuleScript
local module = {}
RunService=game:GetService("RunService")
function getfolder(Name)
	local name = 'Qin2007s crafting recipe_'..Name
	local folder = game.ReplicatedStorage:FindFirstChild(name)
	if not folder then
		folder = Instance.new("Folder")
		folder.Name = name
		folder.Parent = game.ReplicatedStorage
	end
	return folder
end
function module.new(CraftType,recipe)
	if recipe and recipe.In and recipe.Out and recipe.Name and RunService:IsServer() then
		--local folder = getfolder(recipe.Name)
		if CraftType == 'Chance' then
			return require(script.Chance).getRecipe(recipe)
		elseif CraftType == 'Instant' then
			return require(script.Instant).getRecipe(recipe)
		end
	else
		error('recipe.In or recipe.Out are not in the areguments or this is called on the client')
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
about the same as the scripts in this topic

# isn’t reliable especially inside of dictionaries for whatever reason but lua doesn’t give any built in way to see the size of a dictionary

alternative

local function GetTSize(t : {any})
  local a = 0
  for i,v in pairs(t) do
    a += 1
  end
  return a
end

local t = {
  ['hi'] = 'hello',
  ['thing'] = 'something'
}

print(GetTSize(t)) -- 2

no you forgot to make a function

code
local function GetTableSize(Table: {any})
  local Size = 0

  for _ in pairs(t) do
    Size += 1
  end

  return Size
end

local Table = {
  hi = 'hello',
  thing = 'something'
}

print(GetTableSize(Table)) -- 2

EDIT:
you fixed it, so ima make this hidden code

This is a bug that has been annoying for me too, the best alternative I can give would be defining the tables first, and putting those variables in a table:

local tab1 = {var = 1;}
local tab2 = {var = 3;}

local tables = {tab1; tab2}

This is because # only works on lists.

It looks like you’re testing to see if the table is not empty, this can be done with next(t) which will return the first value in a table if its not empty, and is a lot faster than iterating through the entire table to get it’s length

1 Like

The length operator only works on arrays not dictionaries (it uses the final index of an array to indicate how many items/entries are in that array) as dictionaries use fields/keys and not indices the length operator is not compatible with them.