Hey! I’m having a bit of a struggle trying to figure out how to add new values and remove old values from a player’s Data Table that is saved to their UserId. Let’s say this is a fresh Data Table for new players.
return {
Leaderstats = {
Credits = 0
},
}
A new player joins the game, and this is the data that is returned to later be saved with their UserId key for their data later when they join back, obviously, pretty self explanatory.
Let’s say we add a new value though, like XP! The new table will look like this,
return {
Leaderstats = {
Credits = 0,
XP = 0
},
}
Only for new players though.
How would I go about adding (or removing) the new values to previous players already with a saved table, without destroying any of their current data? (I’m not struggling to save already existing values in their table).
I’ll explain or elaborate more if necessary.
2 Likes
Saving will overwrite any previous data, so it will add it in just fine. The part you should be more concerned about is loading old data. If you don’t handle it properly, the server will attempt to load expecting data that doesn’t exist due to outdated data.
Saving will overwrite any previous data,
But I’m trying to add new values to a older player’s table without overwriting any of it’s older data.
If you’re saving when the player disconnects, then the data will already have loaded. So you’re not losing any data. Just make sure to have a check to see if the player’s data has actually loaded fully before attempting to save. Also I’m talking about using SetAsync
rather than UpdateAsync
.
Yeah but if their table is missing a new value, it won’t be added to their table. I need to update their current data with new values without overwriting old data. I’m struggling to get what your saying. Yes when they disconnect it saves their data, but that is relevant?
I’ll try to explain this more visually.
- Player joins
- Handle loading function
- Player disconnects
- As would normally do, overwrite data with new data when saving
When saving, you’re already replacing the old data with new data. This is no different. You don’t need to do anything special except to handle missing new data when loading. You aren’t losing data. The data has already loaded into the game. You’re just taking that data and putting it back into the datastore with a new value. There are no extra steps on the saving part.
Okay, got it. Guess my question really is now, is how to actually add new values. I’ve tried comparing the player’s data to the new player data table, but it just tells me that the player has none of them.
local function updateMissingData(player)
local FreshData = newData()
for i, val in pairs(FreshData.Leaderstats) do
if not ServerData[player].Leaderstats[i] then
print(i)
end
end
end
Output:
Credits
XP
When it should only say they’re missing XP?
I run this after their data has loaded.
Are you sure that their data was reset before testing? I’m not really sure how to answer this as I don’t know how you handling any of this. But the basic idea is to check each value. If it exists, run with it. If not, give it a default value. Obviously if there’s no data period, load from a default table for a new player. What you have right now is basically what it should be. But I would use value = data or default
. Sorry if I’m not explaining this right or understanding your question. I am extremely tired right now and may get off very soon.
This is all that’ll run after the player has loaded their Data, or they don’t have data, either way, this is crucial to when they join.
if not Data then
Data = newData(player)
print(player.Name.." Did not have data, creating a new set now.")
end
ServerData[player] = Data
updateMissingData(player)
On the line, where I say ServerData[player] = Data
, that is so I can edit and change their data while they’re on the server. Then I simply save their table in ServerData when they log off.
Does that give a bit more inside on how exactly I’m handling this?
So if I’m understanding this correctly, you’re basically just taking the array that’s loaded from the datastore and then saving it again when they leave? Note that this array (or dictionary) is not the original, but a clone of it. So just save data with the new values. Then when old data is loading, check each value to see if it exists and give it a default value if it doesn’t exist.
Basically I’m taking the data that was loaded, and saving it to a Server Table where it can be called and modified, then, when they log off I simply save the New Table as their data. So it’d be like JUST this,
DataStore:SetAsync("test11"..player.UserId, ServerData[player])
then, to clear up the server ofc,
ServerData[player] = nil
Above, you see how I’m trying to compare the NewData table to their current data in the Server Table to find missing values, did I do something wrong with my loop or something? I make sure their data is loaded first of course before running the updateMissingData(player)
function.
It looks right to me, so I’m not quite sure where the issue is. My only guess would be something wrong with the saving somehow. Are you encoding and decoding with JSON when saving and loading?
Not that I’m aware of, nothing extreme about my loading and saving data.
Have you had success with loading data before? I’ve noticed that DataStores just refuse to save dictionaries without first encoding them.
Data loads and saves successfully every time, but I cant simply run a loop and compare tables…? That’s got to be where the issue is, I’m not sure whats up with it.
Unless there’s something else going on, it should work. I don’t see anything wrong with the code you’ve posted here.
I appreciate your help, guess I’ll just need to desperately keep trying lol
Sorry I couldn’t find your issue, but I wish you luck. I must now sleep as I’ve been up for like 20 hours.
1 Like
I just got it fixed! I checked this way,
if ServerData[player].Leaderstats[i] == nil then
Rather than,
if not ServerData[player].Leaderstats[i] then
and it works, Appreciate the time and help man!
1 Like