Why is # used in scripting?

So I was learning from some scripts and I’ve just found that the hashtag (#) is used in a common way. So I’ve was thinking,
What does # do for your scripts?

If you know, Please do answer! :smiley:

It’s the length operator, used for getting the amount of index-value pairs in tables and length of strings.

2 Likes

#{table}, will result the number of entities in a table.

#{1,2,3} -- returns 3

4 Likes

It basically just represents the amount of items in a table. For example

local Table = {"Hi", 10}

print(#Table) -- 2
2 Likes
local players = game.Players:GetPlayers()
print(#players)--prints how many players are in the game
3 Likes

The # operator does not count all of the non-nil keys. It counts how long, from the key value 1, until it reaches a nil value. So, if 2 was nil and 1 had some value, it’d return 1.
https://en.wikibooks.org/wiki/Lua_Programming/length_operator