Log in to vote

Best way to mass save data?

Asked by
Cuvette 222
11 months ago

Hey, before I even try putting 60 or so items into a datastore to load for each player when they buy one of them items from my shop GUI. What would be the best way to go around it?

For example, say I had a shop in which players could buy sword (Let's say I have 60 swords to choose from). The player then buys 10 of these swords, and when they load up the game again after closing it instead of the shop asking for them to buy it again it just says 'Equip' or so.

I'm not asking at all for someone to create a script for this but I would just like to know they best way around it. Surely storing all of these items in a datastore would get a bit complicated and possibly laggy?

Thanks, Cuv

maybe save a list of the swords that they contain through the use of 1 variable and some string concatenation ProfessorSev 220 — 11mo


Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.




Now back to your regularly scheduled Scripting Helpers....




1 answer

Log in to vote
Answered by 11 months ago

Background Ok so I'm guessing you know how to save so I'm not going to cover that what I'm going to talk about is string concatenation and how it can be useful when dealing with unknown quantities of data.

What we know -We know each player will have unknown amounts of unique swords -I will also say that each sword has a unique identifier like "SwordA113" or something along those lines

Approach Initially you might want to save a table containing a list of all the swords in the game with a true false value however this is simply inefficient in the long run. So the way that we will tackle this problem is save each swords name if the player has it. So an example of what we would save might look like this...

playerSwords = "SwordA113/SwordDarkness/"

That simply stores the swords names into 1 easy variable simple enough right? Notice the "/" characters offsetting the names? This is how we will separate the names later when we go to retrieve them. So lets start with adding a sword to this list because we can't save data if we don't have anything to save.

local playerSwords = ""    -- This would be declared at the beginning of the script and will act as a global variable 

function AddSwordToList(sword)
    playerSwords = playerSwords + sword + "/"

This code here now is the start of our program we can use this function here to add our sword to list. And When you make your game you may have a few more actions that need to be completed and I would Advise keeping them out of this function so the code stays organized.

Like I said at the beginning I am not going to teach how to save and retrieve here so our next function that is required is being able to break the string up once we have retrieved it. So naturally we create a few more functions to handle this. Before I continue I am going to mention that a full list of how each sword is saved must be present somewhere within your game or script for reference of if the swords that we saved exist, This can be anything like the tools in server storage or whatever your using just know that this next part requires that otherwise it will not work like intended.

given data

playerSwords = "SwordA113/SwordDarkness/LongSword/"

now the function to break them apart

function findSwords(swordList)
    length = string.len(swordList)
    currentPos = 1
    lastPos = 0

    for i = 1, length
        if string.sub(swordList, i, 1) = "/" then -- we have reached the end of a name
            local sword = ""            
            currentPos = i
            sword  = string.sub(swordList, lastPos + 1, currentPos - lastPos - 1)
            lastPos = i

            -- then search through your list in the game or code to then find a match if a match is found the activate it for the player simple enough. 

An explanation for what we did.

So in that last function we searched the whole list that we saved and used the character "/" as a separator to distinguish swords. Instead of searching the string for names we searched for the separator as it gives us a position on where that name is in the full string since we won't always know for each player. Next we used some simple math to find the length of each sub string and the string.sub function to grab the name into a temporary variable that we can now compare to our full list of swords and activate.

hope that this helps.

Outstanding explanation! I never actually considered using string concatenation for it, but that would be an excellent way to break things up. It will also save me time and resources. Once again, thank you for that answer! Cuvette 222 — 11mo
you're welcome and I also find it an interesting way of saving data ProfessorSev 220 — 11mo
Are you saving this with DataStoreService? ScriptGuider 3498 — 11mo
I didn't cover the saving part specifically so that's your call ProfessorSev 220 — 11mo
Extremely inefficient method. Use JSON tables or at least use a built-in means of iterating through parts of a string like gmatch or gsub. Narrev 1067 — 11mo

Answer this question