What are the benifits of saving stuff as IDs vs strings?

Hey everyone. I’ve spoken to a few different developers and it seems like most of the ones I’ve talked to use IDs to save things to their games. So, for instance, for an inventory, the ID in a table for a sword would be 5 and the inventory would save as inventory[“120391”] = {Id = 5}. (depending on how they organize stuff)

I currently have a fully working save system using only strings. If I wanted to save something to my inventory, I would save it as inventory[“Item1”] = {“Weapon”, “Sword”}. It would look inside a table for [“Weapon”], then for [“Sword”] then return the contents (Which in my case would overlap both the actual sword as a weapon and it’s stats, descriptions, etc)

From what I can tell, the only benifit of using IDs is that it saves on datastore space. Which I don’t reckon that many people ever even have to deal with.

On the other hand, the benifits I see from using a string-based systems are that, 1, it is very easy to change values around, instead of having to find the specific ID for something I can just call it by it’s name (So instead of a red shirt being id 3921 in inventory[“12048”] it can just be [“Top”] = {“Top”, “RedShirt”}). 2. It simplifies the process of getting your saved data and generally makes it all around easier to work with. 3. More depending on the person, but in my case it massively declutters my code.
Probably a few more reasons too.

So what is the appeal of using IDs instead of strings? If there is a very important reason for it, I don’t think it’s too late for me to go back and rework my code. A dev has told me that it is better to use IDs, but I have forgotten to ask him the reason why.

1 Like

Basically using numbers to store data uses less space, hence compression.

Sword has five Characters where 1 has one character.

You don’t really need to use this for managing player data but only encode data when saved to datastore and decode it when getting from datastore.

You would also be better off storing Arrays rather than Dictionaries to datastore

You can structure your code smartly to make encoding and decoding really fast and easy as well

It makes sense if you were using it purely for pre-save compression, however the devs I’ve spoken to just flat out use the IDs for everything. The sword would literally be referred to as “5” in the list of items.

Excluding the inventory I have saved more or less everything and am probably still not even .1% to reaching the datastore character cap.

Saving memory perhaps?

I wouldn’t do it myself as it’s not worth the sacrifice of readability and accessibility of my code, but I would definitely encode and decode PlayerData

You’ll never know what you’ll add to your game and being space efficient is important because when you run out of space you get more problems.

You might want to rename something, if you save it as a string you can’t, in a case that you made a typo (you could but it’s extra code)

Faster loading times as well, probably it make sense that less data should take less time to fetch but maybe this isn’t the case for Roblox

But tbh if your game doesn’t require a lot of stuff to be saved then you wouldn’t really have a problem with going over 260k character limit

3 Likes

I probably won’t bother with encryption since it seems like work that will never be put to efficient use (and to be quite frank I am too lazy to make the encryption)

Names for ingame purposes and the actual names that point to stuff in the tables are separate. So even if I wanted to rename it I could just change what it’s called in game and not what it’s called in save files. However, it is still a good point.

Never had an issue with lag. Maybe roblox has gotten better with it over time, but I can’t really see a difference. The loading screen in game right now is purely in place to make sure the light effects have time to load on the client.

I’ll keep the renaming thing in mind for my future projects.

1 Like

I wasn’t talking about Encryption,

Encoding and Decoding meant as compressing and decompressing

You don’t Encrypt with JSONEncode.

2 Likes

Oh wrong word, sorry I’m tired. I meant like, having a large function that turned common letter combinations into numbers instead. However, I don’t really see how it could be turned into less characters without manually implementing every scenario as a number (like turning “Sword” into 23)

edit: which would let you rename stuff easily but would also require a lot of extra work

edit 2 : I think a superior method to doing this over encoding is simply store stuff as their ID and simply get them and write to them using a sorting algorithm that takes strings as an input.

1 Like

People have already said this but if you use id’s versus strings you will have a much better time as you will take up less data, easier to load in and has less chance of corrupting, there is no negative side affects unless you have to change all your code for this system but it is worth it if you are going to have a large game.

1 Like

Don’t know if anybody mentioned this but if the dev ever changes the string name of the item or weapon then it can use the ID to get the same weapon. This makes it changeable and not stuck with the name it was originally assigned.

1 Like

Okay so I’ve changed it to be more ID-like, they save to an array completely made up of IDs but there is one thing that nags me. How do you get the position of a value in an array?

I would like for the string to just redirect to the array and then save the position of that array. I haven’t found a way to find the position of that array so right now I’m storing everything in a dictionary instead and use an array to point towards that dictionary, which means I have an array that only stores a name and a dictionary that also stores that array position which seems very inefficient. Is the only option to reiterate everything until it finds the right array?

Edit: now it says I can’t mix array types when encoding…

1 Like

I’m going to be honest about how I feel about this. Weigh the cons and the pros of each decision.
Ask yourself why using IDs is better than using names.

Most of the points listed here are not wrong, but they probably don’t apply to you. Increased load time, and lag? No chance. If your pc can’t handle 260k characters then you should definitely consider getting a better one. It’s literally 0.26 MB of RAM if you use 100% of the allocated space for each key.

Find a way that you think will be appropriate for the future. If you know that you’ll have to rewrite it in the future if you want to add more to it, why not just make it bulletproof in the first place?
I mean, if it was up to me. Yeah, I would use IDs, not because of load time concerns, but because of simplicity also including what @Beartikal mentioned. If you change the name of your items? Maybe the system you made gets broken!

But, if you are concerned about using too much data, then find a good way to cope with it. Don’t save unnecessary data, such as what the ID belongs to. Consider the code below:

local PlayerDataStore = DataStore()
PlayerDataStore:SetAsync({
    items = {
        [123] = {
            name = "whatever"
        }
    }
});

Do you find it necessary to save the value belonging to the id? I don’t, I’d rather create a table with the value of each id. Something like this, perhaps?

local idValues = {
    [123] = {
        name = "whatever"
    }
};

Then, I can just save the id, and not the redundant data.

Unless you have more than trillions of unique items in your game (and even then) you can assume indexing numbers takes constant time.

On the other hand, indexing a string has linear complexity by the length of the string. If I had the item “SwordSword” it could take about twice as long as just indexing “Sword”. If you index stuff a lot, it could hurt your performance pretty badly.

I can see a fix for this, index everything upon the game starting and add a dictionary called [“Item”] = id or whatever to everything in the array. With this you could save something and treat it as an id and still not really suffer from any index lag. Index lag would only occur upon the server starting, and if it’s still bad you can always just export it like that and not even run the index code.

Edit: Just implemented this, works as well as any string indexing.

image
I don’t think there’s any unnecessary data here.