How to know Amount of things in a table

How can I know how many things are in a table and print it?

local tbl = {}

1 Like

Use the # character before the table.

local tbl = {"Pie", "Napples", "Are", "Tasty"}
print(#tbl)
1 Like

Sorry, i explained it wrong, I meant how to know how amount of things are there In Numbers

I must’ve misunderstood your initial question. My answer would return a number for you to use like you said, unless you want the sum of numbers in a table?

Example using my previous response:

local players = game:GetService("Players")

local playersTable = players:GetPlayers() -- a table
local numPlayers = #playersTable -- gives you a number

print("There are", numPlayers, "players in the server.")

Or if you meant a table full of numbers, and you want the sum (them all added together), you could do:

local tbl = {}
local sum = 0
for _, number in pairs(tbl) do
    if type(number) == "number" then
        sum += number
    end
end
print(sum) -- All numbers in 'tbl' added up.
3 Likes

Yes, that’s what I meant, my idea was to know how many players were on the server, thanks

Another question, how could I do that if there are a maximum of 2 players on the server, something is activated/started? Would it be like this?

local players = game:GetService("Players")

local playersTable = players:GetPlayers() -- a table
local numPlayers = #playersTable -- gives you a number

if numPlayers => 2 then
 print("start")
end

You could stall the script until there are 2 or more players. Something like that would look like this:

local players = game:GetService("Players")

while #players:GetPlayers() < 2 do
    task.wait()
end

print("Start")
-- Code below here will run once 2 players are in the server
1 Like

Then you should have stated that in the subject of the thread’s post. The first reply to this thread was valid the length operator (#) when used preceding a literal table value/a variable which is a reference/pointer to some table value will return the total number of items/elements/values which exist within that table.

#workspace:GetChildren() --number of immediate child instances of the workspace
#{"hello", "world"} --number of values in this literal table (2)```

Bare in mind that use of the # (length) operator before a table which is not an array (an array is a table indexed by sequential keys), a dictionary (a dictionary is a table indexed by custom keys/fields) then the total number of items/elements/values in that dictionary will not be returned (nil is returned).

More information:
https://developer.roblox.com/en-us/articles/Table