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: Moving position in table?

Previous Thread :: Next Thread 
CarlosMX is not online. CarlosMX
Joined: 30 Apr 2011
Total Posts: 791
15 Mar 2017 05:14 PM
This has me stumped... say a string is already inserted into a table (table.insert(Table, 1, "Person")... and assigned it's value inside of the table (Table[1]), is there a way to move that "Person" to another area in the table?

Table = {}

table.insert(Table, 1, "Person1")
table.insert(Table, 2, "Person2")

And now you want to assign "Person1" to value "2" inside of the table... is there a way of doing this? Or would you just have to locate "Person1" inside of the table, remove them, then reinsert them into the table with the value?
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
15 Mar 2017 05:15 PM
Table[1], Table[2] = Table[2], Table[1]


Report Abuse
CarlosMX is not online. CarlosMX
Joined: 30 Apr 2011
Total Posts: 791
15 Mar 2017 05:17 PM
Simple enough.. cheers
Report Abuse
Gomlsauresrex is not online. Gomlsauresrex
Joined: 25 Mar 2016
Total Posts: 3588
15 Mar 2017 05:17 PM
local tbl = {"1", "2", "3"}

tbl[1] = 3
tbl[3] = 1

print(tbl[1])
print(tbl[3])
Report Abuse
Sacrificable is not online. Sacrificable
Joined: 11 Sep 2013
Total Posts: 271
15 Mar 2017 05:18 PM
Table[DesiredValue] = Table[CurrentValue];
table.remove(Table, CurrentValue); --if you're not assigning keys to the table
Table[CurrentValue] = nil --if you are assigning keys to the value and aren't using consecutive numerals.


18,821
Report Abuse
Sacrificable is not online. Sacrificable
Joined: 11 Sep 2013
Total Posts: 271
15 Mar 2017 05:20 PM
you could also copy it to another position

Table[DesiredValue] = Table[CurrentValue];


18,821
Report Abuse
CarlosMX is not online. CarlosMX
Joined: 30 Apr 2011
Total Posts: 791
15 Mar 2017 05:20 PM
Wait hang on... I'm like literally trying to reposition the value, such as if you were to insert a new string to the table it moves the rest of the elements down if you were to put it in front of them...

table.insert(Table, 2, "Person4") < would move Person2 who was originally here down to Table[3]..
I'm not trying to reassign the value of the number...
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
15 Mar 2017 05:22 PM
The table.insert() and table.remove() functions automatically prevent holes in arrays. It should work fine.


Report Abuse
Sacrificable is not online. Sacrificable
Joined: 11 Sep 2013
Total Posts: 271
15 Mar 2017 05:22 PM
local value = table[CurrentPosition];
table.remove(table, CurrentPosition);
table.insert(table, DesiredPosition, value);


18,821
Report Abuse
CarlosMX is not online. CarlosMX
Joined: 30 Apr 2011
Total Posts: 791
15 Mar 2017 05:25 PM
Cheers heaps everyone :)
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