Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does it mean when there's a "#" next to a name of a variable?

Asked by 2 years ago

Hi, I wanted to ask what it means when there's a hashtag right next to a name of a variable like this example:

local players = game.Players:GetChildren()
local playerPositions = {}

if #Players then --what does this mean
      for _, Player in pairs(players) do
            table.insert(playerPositions, Player.Name)
      end
end
0
When doing :GetChildren that variable turns into a table since :GetChildren() returns a table of the children of an instance MarkedTomato 810 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

The # if used before a table will returns the length of the table. This can be used in many cases as I listed a couple below.

local table = {1, 1, 1}

print(#table)

-- Expected output 3
local loadedPlrs = {}

-- This really wouldn't be necessary just an example

for _, plr in pairs(game.Players:GetPlayers()) do
    table.insert(loadedPlrs, plr)

    print(#loadedPlrs .. " Players loaded!")
end

In your case it looks like it checks if any players exist before running that code.

0
Thanks, this helped me a lot since I've read books about Roblox Lua and I've seen examples where they used this. The reason why I made this post though is that they never really explained what the # does very well. AdamBolt2 7 — 2y
Ad

Answer this question