Best way to move dictionary data one key over

I’m making a global donation leaderboard that features the players with the highest donations. I’m doing this by storing all of the player’s information in a dictionary, though I came across a problem.
When a player donates and becomes the #1 donator, I need to make it so that the previous top donator and all of the other ones featured on the list move a rank down. I’m not quite sure how to do this in the most effective way.

Here’s how I have my dictionary organized.

local activeGDT = {
   ["slot1"] = {  
      UserId = "";
      AmountDonated = "";
   };
   ["slot2"] = {
      UserId = "";
      AmountDonated = "";
   };
   ["slot3"] = {
      UserId = "";
      AmountDonated = "";
   };
}

What is the easiest and most effective way to move all the data from slot1 to slot2 and slot2 to slot3 and so on?

You can just activeGDT["slot"..slot] = activeGDT["slot"..slot-1] given slot > 1. If slot is 1, you’d have to get the last slot’s number instead. Though to be honest, an array-like table is much better for this as it can be iterated and sorted and there’s built in methods to add new data to a specific position.

For example:

local x = {
  {UserId: "", Amount: 0},
  {UserId: "", Amount: 0},
  {UserId: "", Amount: 0},
}

You can use table.sort to sort the table by each value’s Amount value. Or you can iterate until you reach a point where the current value’s Amount is less than whatever amount you want to insert and then insert using table.insert(x, pos, data)

2 Likes

I agree with the array part, dictionaries aren’t even sorted by an order so I don’t know if what your doing is gonna work out easily.

You could just swap the key values with the next “highest” key, I assume you’re gonna use rank numbers as the keys.

1 Like

This would be worst way to do this to be honest, why don’t you store donated value inside of just one table and save it through Datastores then use GetOrderedDataStore to make it a global leaderboard depending on how much they donated. If you don’t know how to make a global leaderboard take a look at this: How to make a simple global leaderboard

1 Like

Alright, Instead of using a normal DataStore, use a OrderedDataStore and save the data to there, with the key as their userid. It’ll get the top donators without going through this trouble.
@AtomTostcuOzgurUsta beat me to it lol

1 Like

Is there a limit to how much data you can put in an OrderedDataStore? I tried to avoid making a value for each player because it would lag and be inefficient in large amounts.