generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Cloning tables

Previous Thread :: Next Thread 
Barbariancreep is not online. Barbariancreep
Joined: 23 Nov 2012
Total Posts: 1096
29 Dec 2015 01:37 PM
Because 'local proxy = t:Clone()' doesn't work
Report Abuse
Threatboy101 is not online. Threatboy101
Joined: 27 Oct 2012
Total Posts: 874
29 Dec 2015 01:41 PM
If you're trying to clone individual things inside the table you would do

for i,v in pairs(table)
v:Clone()
end
Report Abuse
Threatboy101 is not online. Threatboy101
Joined: 27 Oct 2012
Total Posts: 874
29 Dec 2015 01:42 PM
for i,v in pairs(table) do end

forgot the do
Report Abuse
LarzCaetano is not online. LarzCaetano
Joined: 26 Aug 2015
Total Posts: 280
29 Dec 2015 01:44 PM
shouldn't it be:

local t={};
local n={}
local v=t:children();
for i=1,#v do
n[i]=v[i];
end

?
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
29 Dec 2015 01:48 PM
Since tables are passed by reference and not value, you have to manually copy each element of a table in a loop, something like this is what I use:

local t1 = {0, 1, 2, 3}
local t2 = {}

for i, v in ipairs(table1) do
t2[i] = t1[i]
end

Report Abuse
Threatboy101 is not online. Threatboy101
Joined: 27 Oct 2012
Total Posts: 874
29 Dec 2015 01:49 PM
I did not know he meant literally cloning the actual table in that case you can simply do

table1 = {'bruh'}
Table2 = table1
Report Abuse
rvox is not online. rvox
Joined: 18 Feb 2011
Total Posts: 5380
29 Dec 2015 01:51 PM
^ no
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
29 Dec 2015 01:53 PM
Threatboy, the problem with doing that is that you aren't actually copying the array, you are just assigning two different variables to the same set of information (arrays aren't passed by value like other basic data types)

That can cause problems; if you need to modify one of the arrays, you will unintentionally modify both of them. You need to manually copy each element of the array if you want to modify one of them and keep the other the same.
Report Abuse
sycips is not online. sycips
Joined: 21 Mar 2011
Total Posts: 1368
29 Dec 2015 02:03 PM
@Threatboy101
That would actually set a pointer to the same table... This is what I mean by that:

Table1 = {"TEST"}

print(Table1[1]) --> TEST

Table2 = Table1
Table2[1] = "CHANGED"
print(Table2[1]) --> CHANGED

print(Table1[1]) --> CHANGED

By doing Table2 = Table1, both variables are actually THE SAME table. Both variables point the same table!

Now for the solution. There is no table manipulation function to clone a table, so we can create out own function:

function CloneTable(Table)
local NewTable = {}
for i,item in pairs(Table) do
NewTable[i] = item
end
return NewTable
end

NOTE that the items in the table will not change. The only thing this does is creating a new table which point to the same items. BUT, when you change this second table, the first table wont change anymore! (Don't try this with objects in a table. If you remove an object where both tables are pointing to and you remove that object, ofcourse both tables wont point to it anymore...) Let me show you:

Table1 = {"TEST"}

print(Table1[1]) --> TEST

Table2 = CloneTable(Table1)
Table2[1] = "CHANGED"
print(Table2[1]) --> CHANGED

print(Table1[1]) --> TEST

I hope this explaination helped you and I hope you understood the difference between variables and tables pointing to values and variables actually being values.

Good luck with scripting! :D

~sycips~
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image