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: Sorting items from smallest req level to largest

Previous Thread :: Next Thread 
C_Sharper is online. C_Sharper
Joined: 03 Oct 2011
Total Posts: 6405
25 Oct 2016 11:29 AM
So say we have a data model containing different models with numbervalues in them called "RequiredLevel"


Like


WeaponInfo:

-Pistol (Required level = 1)
-Shotgun (Required level = 5)

And so on.


However I totally forgot when I rendered buttons for every weapon you can buy, it's strange to have a common weapon appear, then a lvl 100 required weapon right after that. I need to figure out how to add every weapon to a table according to their required level value. From smallest to largest. Trying to figure this out :p

Thanks for reading!
Report Abuse
FrankieTheDino is not online. FrankieTheDino
Joined: 27 Jul 2008
Total Posts: 603
25 Oct 2016 11:33 AM
I would transfer them into a table:

local Weapons = {}

for i,v in pairs (WeaponInfo:GetChildren()) do
Weapons[WeaponLevel.Value] = v
end


and then I would use table.sort:


table.sort(Weapons)


Report Abuse
C_Sharper is online. C_Sharper
Joined: 03 Oct 2011
Total Posts: 6405
25 Oct 2016 11:35 AM
Oh brainfart! Totally forgot about the possibility of dictionaries. Thank you!
Report Abuse
C_Sharper is online. C_Sharper
Joined: 03 Oct 2011
Total Posts: 6405
25 Oct 2016 01:51 PM
Erm, that doesn't work :/



This is what happens when I print everything in the table:

Number = key, and the gun = value.


1 Pistol
45 Ray Gun
3 Shotgun


Should be

1 Pistol
3 Shotgun
45 Ray Gun
Report Abuse
C_Sharper is online. C_Sharper
Joined: 03 Oct 2011
Total Posts: 6405
25 Oct 2016 01:59 PM
Meh, I figured a method, kinda inefficient though.

I sum up all the values of the keys, then use a for loop to run from 0 - X amount of times, gathering all the weapons from 0 - X.


Definitely not the best way. But it works.
Report Abuse
FlamedSkull is not online. FlamedSkull
Joined: 23 May 2009
Total Posts: 1379
25 Oct 2016 02:00 PM
Is that what you're table looked like before?

I've never used sort before but I'm guessing this may work:

local Table = {}
Table = table.sort(Table)


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Oct 2016 02:03 PM
local weapons = {
{ "SMG", 10 }
{ "Pistol", 3 }
}

table.sort(weapons, function(v1, v2) return v1[2] < v2[2] end)
Report Abuse
C_Sharper is online. C_Sharper
Joined: 03 Oct 2011
Total Posts: 6405
25 Oct 2016 02:03 PM
Thanks for replying Skull.


I used table.sort and it didn't work when I tried it.

Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
25 Oct 2016 02:03 PM
Weapons = WeaponHolder:GetChildren()
table.sort(Weapons, function(a,b)
return a.RequiredLevel.Value < b.RequiredLevel.Value
end)


Report Abuse
C_Sharper is online. C_Sharper
Joined: 03 Oct 2011
Total Posts: 6405
25 Oct 2016 02:04 PM
Thanks cnt, I'll try that!
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Oct 2016 02:05 PM
If yours is set up the way chimmihc put it, use hise
Report Abuse
C_Sharper is online. C_Sharper
Joined: 03 Oct 2011
Total Posts: 6405
25 Oct 2016 02:06 PM
Lol I can't keep up with the replies.

Thank you chimm :)


And while I'm waiting for the reply cooldown to finish I'll just say thank you to anyone whom posts while I'm typing this. :P
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