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.
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
stringtable.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.
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.
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)
end
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?
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).
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)
end
end
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