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)
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
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)
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.
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.
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.
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!