I want to move all of the values of one table to another table. Is this possible?
TableA = { Colors = {“Red”, “Orange”, “Yellow”, “Green”, “Blue”} Numbers = {1, 2, 3, 4, 5} Letters = {“A”, “B”, “C”# } TableB = {} table.move(TableA, TableB) print(TableB)
Given that the previous answer is wrong, has several typos and the post isn't that old I figured I'd give answering it a try.
The function is a little more like 'Normal' languages and doesn't hold your hand as much which is why it may be a little more confusing. It's especially noticeable (And there's almost no documentation explaining this) that it will actually Override values in a table, rather move them.
The *first* argument is the table to overwrite the second with.
The *second* argument sets the start index within the first table.
The *third* argument sets when to stop within the first table.
The *fourth* argument defines where within the second table to start overriding.
The *fifth* argument is the table to be overridden.
Note: The fourth argument cannot be negative due to how the function works, thus in the example it is set to the second tables length plus one because of how Lua starts at 1 instead of 0. If you leave the fifth argument blank then it will override itself, in example setting the fourth argument to 3 will give "12123456" since it overrides the third index until the end of the original table.
local Table1 = { Numbers = {"1", "2", "3", "4", "5", "6"}, Colors = {"Green", "Black", "Grey"}, Parts = {"Block", "Sphere", "Wedge", "Mesh", "Union"}, } local Table2 = {"A", "B", "C", "D", "E", "F"} table.move(Table1.Numbers, 1, #Table1.Numbers, #Table2 + 1, Table2) print(table.concat(Table2))
This will result in "ABCDEF123456" because it's set to start at [1] of the first table until the end of that table then start at the end of the second table (Plus one, because again Lua) and begin writing the previous result.
Also note that it won't work on tables that aren't plain arrays, so you'll need to move the previous three individually.
Took a while and some research to figure it out. Hope that explains it a bit better, it's more of a quick (Cheap and dirty) way to combine tables without having to for loop or table.insert them although they will give you more flexibility. Having the option to simply append and use it on more types of tables would be really nice but we can't always have nice things.
(P.S - I'm actually EB8699 but there's no way to disable 2FA so lost that account here. Feel free to message me on roblox if you'd like help with any other scripting questions.)
Hello Vex! Yes, you can use table.move
to move it contains to another table. For example:
local TableA = { Colors = {“Red”, “Orange”, “Yellow”, “Green”, “Blue”} Numbers = {1, 2, 3, 4, 5} Letters = {“A”, “B”, “C”#} } local TableB = {} table.move(TableA, 1, 3, 1, TableB) print(TableB)
The first argument is the table you want to move. The second argument is the minimum index you want to move. The third argument is the maximum index to you want to move. The fourth argument is the table you want it to move to. Hopefully, this helps. If it does, please accept my answer.