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?
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 + "/" end
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.
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. end end end
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.