How to get total number of objects that is inside of a Module script

here is a ModuleScript

local module = {
	Task1 = {title = "Go to shop",progress = 1},
	Task2 = {title = "Sell your gems",progress = 1},
	Task3 = {title = "Kill a player in PVP zone",progress = 1},
	Task4 = {title = "Reach level 5",progress = 5},
	Task5 = {title = "Go to Moon",progress = 1},
	Task6 = {title = "Increase your walkspeed",progress = 1}
}

I want to get the total number of the Tasks, so I tried this

local ServerStorage = game:GetService("ServerStorage")
local Module =  require(ServerStorage:WaitForChild("ModuleScript"))
local TotalQuest = #Module:GetChildren()

print(TotalQuest)

but it is not working

The hash operator doesn’t work for dictionaries

local function n (t)
    local i = 0
    for _ in pairs(t) do i += 1 end
    return i
end
print(n (module)) -->

GetChildren is not a valid function of table types.

2 Likes

can you make it clearly , what is “t”?

A parameter, pass a table argument for the function to iterate within.

i thought it should be like this


local function GetTotalQuests()
    local TotalQuests = 0
    for _,Quest in pairs(Module) do
        TotalQuests = TotalQuests + 1
    end
    return TotalQuests
end

print(GetTotalQuests)

That works too, you can name variables whatever you want.
https://www.lua.org/pil/4.2.html

The += is a combined operator by the way in case you’re wondering,

local a = 0; 
a += 1 -- a = a + 1

no it didnt work , it print “function: 0x2244721a99200fae”

The function was written correctly (correct as in it will work for what you want, if used correctly) but wasn’t executed.

print(GetTotalQuests()) -- execute with parentheses

printing a function prints an address.