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

What are square brackets ([]) and hashtags (#) used for?

So while looking through morph scripts I found this:

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Arm1") == nil then
		local g = script.Parent.Parent.Arm1:clone()
		g.Parent = hit.Parent
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = hit.Parent["Left Arm"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			if h[i].className == "Part" then
				h[i].Anchored = false
				h[i].CanCollide = false
			end
		end
		
	end

end

script.Parent.Touched:connect(onTouched)

I'd like to know what the # (eg #C) and the square brackets (eg C[i]) mean. Thanks in advance!

2 answers

The Hashtag(#) symbol is used when referring to a quantity of something. i.e

local aTable = {"Hello","Welcome","To","Scripting","Helpers!"}

for i = 1,#aTable do --The for loop executes depending on the number of items in aTable
	print(aTable[i]) --This prints the string that corresponds with the item number; i.e. print(aTable[2]) would print, "Welcome"
end
--Output:
--Hello
--Welcome
--To
--Scripting
--Helpers!

# is used to find the length of a list or a string. It returns a number.

[] are used to either search for a child via a string, or search a list via a number. It returns the object or value.


Brackets:

Just like a dot, but you can use a string, i.e.

workspace.Part
-- =
workspace["Part"]

Useful if then name of an object has spaces.

--INCORRECT:
workspace.The Part

--CORRECT
workspace["The Part"]

Can also be used to get a value from a table via a number;

local myTable = {"valA", "valB", "valC"}
print(myTable[2]) --valB
print(myTable[3]) --valC
print(myTable[1]) --valA
print(myTable[math.random(1,3)]) --random

If you have a variable for a number, and that variable is changing, you can "loop through" the list;

local myTable = {"valA", "valB", "valC"}
for i = 1,3 do
	print(myTable[i])
end

The GetChildren() method returns a table of all the children of an object, so it works the same way.


Number sign:

It's best not to think of it as a hashtag or the pounds symbol, because in Lua it is used as a number sign. When placed before a list, it return the number of that list. It can also be used for strings; it will return the number of characters.

local myTable = {"valA", "valB", "valC"}
local myString = "Hello!"

print(#myTable)
print(#myString)

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