Hello, The script below... For some reason it says that clone is a nil value? Although it prints that 4 items insterted into the table... Any ideas
01 | for i,v in pairs (storage.items:GetChildren()) do |
02 | if v:IsA( "Tool" ) then |
03 | table.insert(items, v) |
04 | end |
05 | end |
06 |
07 | print (#items.. " Items inserted into the table" ) |
08 | local clone = items:Clone() |
09 | clone.Parent = player.Inventory |
10 | print ( "Cloned" ) |
11 | end ) |
Thank you.
A table isn't an instance that you can just call :Clone() on.
If you want to clone a table, do this:
01 | local first_table = { 5 , 15 , 20 , 25 , 30 } |
02 |
03 | -- Let's say I want to clone this table ^^ |
04 | -- To do that, we need to create another table and iterate through the original: |
05 |
06 | local second_table = { } |
07 |
08 | for i = 1 , #first_table do |
09 | second_table [ i ] = first_table [ i ] |
10 | end |
Now, second_table has all of the values in first_table.
And if you want to put each of their values into a player's inventory, you need to iterate through the table again.
1 | for i = 1 , #second_table do |
2 | second_table [ i ] .Parent = player.Backpack |
3 | end |
I know that second_table contains only numbers, but the same thing can be done with a table that contains tools.
Also, with tables, you can't just do this to clone tables:
1 | test_table_ 1 = { 5 , 10 , 15 , 20 , 25 , 30 } |
2 | test_table_ 2 = test_table_ 1 |
What that does is simply creates another variable for the exact same table in memory. You're not cloning the first table, you're just creating another reference for it.
1 | test_table_ 1 [ 1 ] = 5 |
2 | test_table_ 2 [ 1 ] = 10 |
Since test_table_1 and test_table_2 are referencing the same table in memory, both of these lines are modifying the same index of the same table.
1 | print (test_table_ 1 [ 1 ] ) -- prints 10 |
2 | print (test_table_ 2 [ 1 ] ) -- prints 10 |
Since we changed the first index of the same table to 10 on the last line, and both variables reference the same single table in memory, both of these lines produce the same result.
When you say items:Clone() on line 8, you need to loop through the table and clone the items to the player's inventory individually.
1 | for i,v in pairs (items) do |
2 | local clone = v:Clone() |
3 | clone.Parent = player.Inventory |
4 | print ( "Cloned" ) |
5 | end |
I don't think you can just clone the table and put that in the player's inventory/backpack.