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 = ""
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.
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
local sword = ""
currentPos = i
sword = string.sub(swordList, lastPos + 1, currentPos - lastPos - 1)
lastPos = i
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.
1 comment
maybe save a list of the swords that they contain through the use of 1 variable and some string concatenation