How do you change the key of a dictionary

Well hello there, I’m currently working on a inventory system.

I’m using a dictionary to store my items and the keys used are the location of the item in the backpack, so for example if an item is stored in the backpack B at the slot 24 :

local InventoryData = {["B24"] = item}

basicaly i just want to change the key of the item if im moving it around but if i type this :

InventoryData["B5"] = InventoryData["B24"]

The old item on key B24 doesn’t disappear, and i need to do :

InventoryData["B24"] = nil

I tried to search for an answer but found nothing, so im asking this here, does a line of code exists, which does those 2 actions in 1 line ? (moving the key and deleting the old one)

2 Likes

Can’t you simply transfer the item, then remove it from the previous placement? Basically combining both lines

InventoryData["B5"] = InventoryData["B24"]
InventoryData["B24"] = nil

print(InventoryData["B5"])
print(InventoryData["B24"])

Does this create a problem?

2 Likes

Well I just see a very minor problem doing it this way, because this means that at a moment (a very very brief moment) there will be 2 items existing in game when in fact its actually the same, couldn’t that cause some problems if a player leaves at that moment and I store B5 and B24 in datastore?

Why don’t you just convert InventoryData["Bn"] to InventoryData["B"][n] where n is the index.
This would make it much easier to swap values.
Example:

local InventoryData = {
  A = {5, 2, 3, 6},
  B = {4, 3, 2, 1}
}

-- Swap 5 and 6 in A
local tmp = InventoryData["A"][1]
InventoryData["A"][1] = InventoryData["A"][4]
InventoryData["A"][4] = tmp
1 Like

Well, it should happen very fast that the player can’t notice, but there might sometimes be a delay correct. Maybe what you can do is, store what B24 is in a temporary variable, remove B24, and set B5 to that variable. Like this the object is removed first instead of copied first.

local set 
set = InventoryData["B24"]
InventoryData["B24"] = nil
InventoryData["B5"] = set

(Whoops just realised this is what @Auxintic said as well)

1 Like

i had this idea too but its more values to insert, so if i could do it by using keys, it’d have been easier :slight_smile:
and for your exemple i can do :

local tmp = InventoryData["B24"]
InventoryData["B24"] = InventoryData["B5"]
InventoryData["B5"] = tmp

I think i’ll switch to this order of actions if there isn’t 1 line of code that exists and do both. Because like this if someone ever finds a way to abuse this, well he’ll just lose items. Or i could store it in InventoryData[“0”] and check when a player come if a value is there so he’ll never lose items.

(well no the same problem will appear)

Like for example in python you can do :

dictionary[new_key] = dictionary.pop(old_key)

this will move the value to the new key and delete the old one at the same time in one line

Is there something equivalent to this in roblox studio ?

1 Like

I think you should look more into Lua if you want to dig deeper into replacing keys. Roblox Studio doesn’t really have anything to do with keys. If you look up how Lua tables are handled and what functions they can perform I think you will get better results than by searching the Roblox wiki or API.

There are people here who know what these functions are but I guess they haven’t seen your post. Either way I think it’s best if you use the suggestions above, while searching up the python method you want to use in Lua code.

1 Like

If you desperately want it on one line, you can do

InventoryData["B5"], InventoryData["B24"] = InventoryData["B24"], nil

But there’s not much benefit in doing so for this case. If you’re setting the second one to nil, it’s essentially the same as having them in two lines one after the other. Having them as two lines is clear and will run immediately after one another. The only time the scheduler runs a line of code elsewhere in the game is when your script yields, which it won’t do between these two lines. You won’t have any issues of the item appearing twice, even if the player manages to leave perfectly in the middle of the operation.

Edit: If you are swapping the keys then this method is better, as you’d require three lines with a temporary variable otherwise.

4 Likes

Okay thanks for the info ! By the way, if i have one item on 24 and one item on 5, will using this method just switch them ?

InventoryData["B5"], InventoryData["B24"] = InventoryData["B24"], InventoryData["B5"]

Or will this cause a problem (since as you said It’s the same as having them in two lines) and actually do this ?

InventoryData["B5"] = InventoryData["B24"]
InventoryData["B24"] = InventoryData["B5"]

(yes it works :grin:)
Thx everyone for the answers

Updated my other reply to add a bit of clarity and to address the swapping. It does work and is much better than messing about with a third variable to temporarily store the value whilst swapping!

Glad I could help.

1 Like