I want to print a list of children, but when I print game.ReplicatedStorage.Folder:GetChildren()
, it shows table:
in the output.
**I have tried listing out every single item inside, but it’s a pain and I don’t want to do this in multiple scripts.
Edit: With the new expressive output Beta feature, you can call print
on a table and view its contents within the output.
That’s actually a list since Instance:GetChildren() returns an array, when you call print on a table it shows the memory address.
If you need to access each individual item through the table then iterate.
for _, item in ipairs(game.ReplicatedStorage:GetChildren()) do
print(item.Name)
end
Again, these aren’t random codes but rather are hexadecimal memory addresses for the table.
You can use table.unpack too:
print(unpack(game.ReplicatedStorage:GetChildren()))
Another idea, to print every item when calling print set a metamethod.
local instances = game.ReplicatedStorage:GetChildren()
setmetatable(instances, {
__tostring = function(self)
local names = {}
for _, item in ipairs(self) do
table.insert(names, item.Name)
end
return table.concat(names, ", ")
end})
print(instances)
#--> prints a string containing the names of every object separated with a comma
It’s just a bunch of random codes like this: 0x52790fa4aeb8bed1
Do I have to do something to change it from this code to get the list?
You are just printing the table’s memory address. You need to iterate through the table to show the values, or if they can be converted to strings use table.unpack.
for _, v in ipairs(game.ReplicatedStorage.Folder:GetChildren()) do
print(v.Name)
end
In this example you would be iterating through the table and calling print() for each value individually
print(table.unpack(game.ReplicatedStorage.Folder:GetChildren()))
In this example you are calling table.unpack() on the list which converts it into a tuple of instances. The tuple is then passed into print() which will attempt to convert them into strings.
There’s no way to change the functionality of print
in every script. There’s actually some methods, but I believe most of the convenient methods involve getfenv
and setfenv
which unfortunately disables Luau’s optimizations. The mentioned code samples above are pretty much the simplest you can get. Put the code into a module to maximize reusability.
Turn Log Mode off in the Console.