Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

0

When doing :GetChildren that variable turns into a table since :GetChildren() returns a table of the children of an instance

1 answer

✓ Accepted answer

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.

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.