Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Table.concat not working when checking a name?

Asked by 5 years ago

So I have this code:

local SquareTable = {"Square1", "Square2", "Square3", "Square4", "Square5", "Square6", "Square7", "Square8", "Square9", "Square10", "Square11", "Square12", "Square13", "Square14", "Square15"}
for i,v in pairs(game.ServerStorage:WaitForChild("Parts"):WaitForChild("Squares"):GetChildren()) do
    if v.Name == table.concat(SquareTable) then
        v:Clone().Parent = game.Workspace
    end
end

I'm checking to see in the ServerStorage if the part's name are equal to the Square name.

There are 15 parts in the Squares folder inside of the server storage, and they are all named Square1, Square2, etc. As it follows to Square15. But I'm placing those names in a table and then I'm concating it to get all the values of the table and to see if all the parts in the Squares folder are equal to the SquareTable. However, this does not work. I get no error, i get nothing. So what's the solution? What am I doing wrong?

1 answer

Log in to vote
1
Answered by
legosweat 334 Moderation Voter
5 years ago
Edited 5 years ago

table.concat returns a string of the items in the table.

Ex;

local Table = { "A", "simple", "table.concat", "test" }

print( table.concat( Table ) )

This would return;

Asimpletable.concattest

But why are you getting no error or output.. Well, if you compare the v.Name (Square1) to the cancat table which would be all the names, Square1Square2..etc. it would not pass the if statement- there is your problem.

For this task you want to complete, I don't think table.concat is the best way to come of this.

If you still want to do concat, try to do a gmatch in the string. Ex;

local tableArray = { "A1", "A2", "A3", "A4" }
local tableCat = table.concat(tableArray)
local partFolder = workspace.Squares -- example

for i,v in pairs (partFolder:GetChildren()) do
     if string.gmatch(v.Name, tableCat) then
          -- its in the table
     else
          -- not in the table
     end
end 

In your case you want to check to match the table with the explorer tree-

local SquareTable = {"Square1", "Square2", "Square3", "Square4", "Square5", "Square6", "Square7", "Square8", "Square9", "Square10", "Square11", "Square12", "Square13", "Square14", "Square15"}

local Squares = game.ServerStorage:WaitForChild("Parts"):WaitForChild("Squares"):GetChildren()

-- now you have both tables.. now compare them

if table.concat(SquareTable) == table.concat(Squares) then -- if tables match then
     for i,v in pairs (Squares) do -- itterate thru squares in folder to replicate to workspace
          v:Clone().Parent = game.Workspace
     end
end

If this helped, please accept this answer! :D

0
I'm just trying to check if the folder that contains all the parts, their names are equal to the table. So if they are equal to the table then I want to clone them into the workspace LuaBloxCodeSlash 1 — 5y
0
I edited my answer! This should help you a bit more. Accepting my answer would help a lot, thanks! legosweat 334 — 5y
0
http://prntscr.com/kvjuaf I get this error, it's on the line where you put "if table.concat(SquareTable) == table.concat(Squares) then" LuaBloxCodeSlash 1 — 5y
0
maybe use a wait for you use the if statement legosweat 334 — 5y
Ad

Answer this question