Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 06:44 PM |
| how do you make an array equal another array? or reset it? Im trying to do this in a while loop but a2 = a1 does not work, it only works outside of the loop ;l |
|
|
| Report Abuse |
|
|
|
| 17 Nov 2015 06:45 PM |
Arrays can't be copied that way, the variables for arrays only point to the array.
http://wiki.roblox.com/index.php/Cloning_tables |
|
|
| Report Abuse |
|
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 17 Nov 2015 06:45 PM |
Well first off in Lua they are called tables, not arrays (sorry it's a habit I had to)
But we'll need more context than that, since there is probably a reason specific to your code that a2 = a1 is only working outside the loop |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 17 Nov 2015 06:47 PM |
You can call them arrays if you are using them like arrays, because behind the scenes there IS an array part and a hash part, if the hash part is empty it's technically being used as only an array.
Also OP the comparison operator will compare it by the actual object, not the values inside. A very easy way would be:
if table.concat(arr1, ";") == table.concat(arr2, ";") then
|
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 06:49 PM |
ill try this
local original = {1, 3, 5, 7, 9} local copy = {unpack(original)}
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 17 Nov 2015 06:51 PM |
Oh wait you want to clone the array. Yeah that'll work, I thought you wanted to compare arrays for some reason. |
|
|
| Report Abuse |
|
|
|
| 17 Nov 2015 06:52 PM |
"Oh wait you want to clone the array." Woah, I actually got that right? I was just taking a shot in the dark at the vague question. |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 06:56 PM |
erm so like that only clones the first 4
table.remove(ani,cycle) if #ani <= 4 then ani = {unpack(save)} end i think it has to do with the fact that is while #ani <=4 dont know why though |
|
|
| Report Abuse |
|
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 17 Nov 2015 06:58 PM |
| It would be easier if we had some more context. What are you trying to do? What is cycle, and what are the contents of ani and save? |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 06:59 PM |
save = {1234, 2345, 3456, 4567, 5678, 6789, 7890, 890}
at the time when ani = 4 ani = {1234, 2345, 3456, 4567}
|
|
|
| Report Abuse |
|
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 17 Nov 2015 07:03 PM |
| Well it looks like you're removing values every time your loop runs, and by the time ani is 4, it will have removed 4 values, leaving 4 left |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 07:04 PM |
| yes i know but it should reset it to 8 everytime it hits 4 |
|
|
| Report Abuse |
|
|
62GB
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 4157 |
|
|
| 17 Nov 2015 07:04 PM |
Does this work?
function CloneATable(ToClone) local ToReturn = {} for i,v in pairs(ToClone) do ToReturn[key] = v end return ToReturn end |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
| |
|
62GB
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 4157 |
|
|
| 17 Nov 2015 07:06 PM |
Oh my bad:
function CloneATable(ToClone) local ToReturn = {} for i,v in pairs(ToClone) do ToReturn[i] = v end return ToReturn end |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 07:07 PM |
| ya i caught that error but your script like never ends or something e.e |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 07:09 PM |
| nvm i forgot to remove return but your script doesnt do anything, it ends up making it continue dropping not changing ani at all |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 07:10 PM |
table.remove(ani,cycle) if #ani <= 4 then for i,v in pairs(save) do ani[i] = v end end |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
| |
|
62GB
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 4157 |
|
|
| 17 Nov 2015 07:20 PM |
| unpack() or the code we provided should be working if used correctly. It's a problem on your end most likely. |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 07:25 PM |
| wait i just figured the problem since im using table.remove im removing the value completely so thats why I got 4 constantly |
|
|
| Report Abuse |
|
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
| |
|
62GB
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 4157 |
|
| |
|
Belleq
|
  |
| Joined: 05 Jun 2014 |
| Total Posts: 21703 |
|
|
| 17 Nov 2015 07:52 PM |
wait(5)
save = {1234, 2345, 3456, 4567, 5678, 6789, 7890, 890} local ani = {}
local cycle = 0
ani = save
while true do
cycle = math.random(1,#ani) table.remove(ani, cycle)
if #ani <= 4 then ani = {unpack(save)} end wait(10)
end |
|
|
| Report Abuse |
|
|
62GB
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 4157 |
|
|
| 17 Nov 2015 07:54 PM |
Try..
save = {1234, 2345, 3456, 4567, 5678, 6789, 7890, 890} local ani = {}
local cycle = 0
for i,v in pairs(save) do ani[i] = v end
while true do
cycle = math.random(1,#ani) table.remove(ani, cycle)
if #ani <= 4 then ani = {unpack(save)} end wait(10)
end
|
|
|
| Report Abuse |
|
|