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

How to display the amount of objects in a table?

Hello,

I am currently just working on an inventory UI, nothing special however, I am having problems with having my item capacity spit out the number of items currently in the UI.

local MaxItems = 18
for _,ItemObj in pairs(Container:GetChildren()) do
	if GameItems:FindFirstChild(ItemObj.Name) then
		table.insert(Items, ItemObj)
	end
	print (#Items,ItemObj)
	UI.Frame.ItemCapacity.TextLabel.Text = "Capacity:("..#Items.. "/".. MaxItems..")"
end

Any guidance would be greatly appreciated.

Also, having many more questions to post about tables.

3 comments

0

In lua the length operator is used to find the length of strings or tables # you can find out more about it here: https://stackoverflow.com/questions/17974622/what-does-mean-in-lua

0

So I am basically just generating the number of bytes or what? Sorry, that explanation was a bit over my head.

0

Do you have any errors?

1 answer

There are two ways of doing this, one for arrays and one for dictionaries, for arrays, you can use the length operator , which is #, another , longer method involves the getn function of the table library. However this method doesn't work for dictionaries, which means you would have to iterate through the children of the table.

Arrays:

local arr = {"yes","nope","oof","telamon"}
print(#arr)--4
print(table.getn(arr))--4

Dictionaries:

local dict = {name = "Jeff", Ping = "pong",Pos = "how do i know?!"}
local length = 0

for i,v in pairs(dict) do
length = length + 1
end

print(length)--3

2 comments

0

iirc getn is deprecated

0

it doesn't say that on the wiki though

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