Invalid value (userdata) at index 1 in table for 'concat'

Hi, so I’ve had a problem for a couple of days now, where I am unable to print using table.concat() when printing a table.

children = game.Workspace.TestFolder:GetChildren()

print(children) --> table: 0xc1c2ed34fdde2c76

print(table.concat(children, " , ")) --> invalid value (userdata) at index 1 in table for 'concat'

(Full error: Workspace.Script:7: invalid value (userdata) at index 1 in table for ‘concat’)

The ancestry:

I don’t understand, as it prints a table thing but when trying to do print(table.concat()) it just doesn’t work.

I’m sure it’s a quick fix and that I’ve just done a stupid mistake - who knows. Anyways, any help is appreciated. Thanks for reading.

1 Like

You can only concatenate strings and numbers in Lua(u), unless a __concat metamethod says otherwise. But Instances don’t have __concat. So it is trying to concatenate the instances together. You can use a for loop and print the names though

string table.concat ( array t, string sep = “”, int i = 1, int j = #t )
Given an array where all elements are strings or numbers, returns the string t[i] … sep … t[i+1] … sep … t[j]. The default value for sep is an empty string, the default for i is 1, and the default for j is #t. If i is greater than j, returns the empty string.

I was thinking about printing the whole table. Like explained above. Not concatenate 2 things together.

There is a reason it is called concat, since it concatenates them all together.

Yes, but why doesn’t it work for the table in my example. I’ve used it, probably, hundreds of times and it usually always printed out: Value1, Value2, etc etc.

Because instances can’t be concatenated. If it worked in the past you were probably working with strings or table/userdata with __concat metamethod.

1 Like

Perhaps you need to store the names of each child inside a table.

local tableOfNames = {}
for _, part in ipairs(game.Workspace.TestFolder:GetChildren()) do
    table.insert(tableOfNames, part.Name)

print(table.concat(tableOfNames , " , "))

Oh yeah! That’s right, sorry for that. But are there any other ways, since I am trying to (later in the script) find a corresponding value in the table - and that won’t work too, right?

And the objects/instances get “turned into” strings?

They don’t transform into string, the objects’ names are simply getting stored in a table to use the concat method.

@incapaz @Awesom3_Eric

Because I am interested into using table.find() later on. So, the reasoning that I use table.concat() was mainly for debugging purposes.

I would like to do:

if table.find(testFolder, playerName)  then 
--found the name inside the folder

I’m confused here. Will there be like, values, stored in each folder representing the player?

Sorry for my unclear explanation. So, basically I have a folder full of string values representing each wizard (player). So like, the blue wizards are in the blue folders and so forth.

Then, I would like to show a GUI only to the blue wizards - therefore I am sending a blueWizardFolder:GetChildren() to the client and the client will check if it find the localPlayer.Name inside the “table”. And for that I will be using a table.find() method.

So it work’s perfectly when I just make a “dummy” table to test it out (like, table = {“Player1”, “Player2”}) but when I use this method it doesn’t work. Because, as you stated earlier, the values are not strings, they are stringValues (you know the object, like numValue). And there lies the problem (probably).

Did you understand now? If not I can try again.

I see, I kind of understand now.

A solution for this is sending a table of names rather than a table of objects, that way, the player can locate their name using table.find.

You can create a function the returns a table of names, and fire that to the client.

local function CreateNames(folder)
    local names = {}
    for _, part in ipairs(folder:GetChildren()) do
        table.insert(names, part.Name)

local PlayersInBlue = CreateNames(blueWizardFolder)
RemoteEvent:FireClient(player, PlayersInBlue) -- Or Whatever lol

And in a local script, you can use

if table.find(tableOfNames, localPLayer.Name) then

That probably works, thanks. And I’ll test it out first thing later on!

1 Like