So guys i recently came across an issue that is making my group members and friends mad. My datastore would save they data one time but then the next time it wouldnt i have seen this in other peoples games too i would like to know why?
First of all, please read the advice on posting. At the moment, your question is vague and unhelpful, making it impossible to give an answer.
To reform your question, do the following:
-
Include your code for saving data. We can’t tell you what has gone wrong if we can’t see how you’re doing things. Include, any relevant code to saving data, and give clarification as to how it works.
-
Tell us any output in the developer console. The developer console is there for a reason, to tell you what you did wrong. For us to help, it is essential that you include this.
-
Be more clear about what exactly has gone wrong. How is the data not saving? Is it definitely saving incorrectly, or could it be how you load it? Was there any special circumstances that lead up to the data save failure? Did the data take a long time to load? Please fill us in on any appropriate detail
-
Be more clear about commonness. Does that data fail every single time, or just commonly? Can you even save at all? It’s important to us to form a helpful answer.
-
Although purely cosmetic, please fix the grammar, spelling and remove slang in the title and description. It’s hard to tell at first glance what you’re asking, and that’s not good.
Reply/edit with all of this included, and people may be able to help you better than a stab in the dark.
Depends on the scenario. What output are you receiving? It has before been an issue in Roblox’s end but it’s much more likely that something is going wrong in your game instead.
Maybe you are exhausting the datastore limitations which is causing lots of throttling?
It would be useful to see some kind of error or code so we can help be more specific!
Hmm i mean datastoring does have to save players data
And when my friends join my games they kinda leave in big chunks so that could be the solution?
If all the players are leaving at the same time then it could instead be an issue with the servers closing before the data has been saved. If you could provide more info (@CodeNinja16 's post), that would be cool!
realised you added code
Looking at your code, when the players leave you are saving each type of data in a different datastore key. This isn’t good because you will be using up the limitations I mentioned before very quickly, which will cause big queues and then the server will close and the data won’t save.
You should instead put each stat in a table, and save that once.
E.g.
local dataTbl = {}
for i = 1, #statstorage do -- loop through every stat
table.insert(dataTbl, {statstorage[i].Name, statstorage[i].Value})
-- add {name, value} to our table
end
datastore:SetAsync("MainKey", dataTbl)
-- save once!
Getting data:
local dataTbl = datastore:GetAsync("MainData")
-- get data once
for _,data in next, dataTbl do -- loop through the data table we saved
local name = data[1] -- the name of the stat
local value = data[2] -- its value
-- do whatever you want here
-- add to leaderstats etc
end
EDIT: Datastores getting and setting wont always work. It’s a good idea to use a pcall to catch these edge cases so you can try again!
The way that you’re saving this will quickly exhaust your limits in data store usage, especially with a lot of players.
In case you don’t know already, in order to keep datastores open to everybody on the platform, ROBLOX places a limit on how many times you can write to your data. At the moment, you write all the datastats values individually, for every player. Not too good.
You need to append these to a table and save it in one call, then loop through the table, filling in the leaderstats values when you load. It adds a bit of complexity, but it is a good solution. @mario118118 has offered a rudimentary example above.
Another huge, gaping hole in your data saver is the fact that there is no PCalls in sight. A PCall is a special instruction that does not error out your script if its operation fails. Read up on PCalls here here. PCall also informs you if it fails, so you can inform the user about it, warning them not to leave the server.
Also, if your data is still not saving correctly, try out the guardian angel known as DataStore2. It’s free, open source and prevents a lot of data loss. Coupled with your data serialiser I spoke about above, you should mitigate a lot of data loss.
Another, smaller problem is to do with server shutdown. When the server shuts down, it doesn’t always have time to save all data. An @Alvin_Blox video has something about that particular problem in it, which is available here. The part dealing with bindtoclose
is close to the end.
If you follow all of these steps, you might just stop the data loss (woohoo). Please, reply with a screenshot of your console, still. We really need to see an error message to really tell what is going on. As I stated, things like that are essential.
Thanks never thought of saving data that way never used a table because of how complicated it was but you made it look simple ill try it.
pcall doesn’t tell you of code failures, it catches the error and returns it to the calling environment. You can process the return value of a pcall, if it errors, by checking for false success and handling the error accordingly (sending the error message to the console via warn, prompting the player that there was an issue interacting with their data).
Retry data or block saving requests. Players won’t have much to gain if they’re told not to leave and no rebounding efforts are applied. This isn’t implied and some developers will take that advice at face value, so it’s always good to clarify.
I’m sorry, I wasn’t that clear. What I mean by that is that you can store the result if a PCall and use it to take further action, rather than having your script errored out.
All I mean is, PCall returns some information which allows you to determine if some code ran successfully.
Once again, my error. I meant to put another example of an action. For clarificstion to OP, use the result if the PCall to take appropriate further action. Don’t use my example as a concrete solution.
Thought I’d mention to stop using :SetAsync and instead use :UpdateAsync. This may solve your issue as it did mine when I was having DataStore issues.
Example:
DataStore:UpdateAsync(Player.UserId.."/CASH", function()
Cash = Player.Data.Cash.Value
return Cash
end)