Yea it’s not printing any of those with the pcall, or the warn function at the end.
Just to check if thats the problem, place this at the bottom of your script:
game:BindToClose(function()
task.wait(5)
end)
It will cause to your studio to wait 5 seconds before actually close, it will give enough time to datastore to be saved.
Its just a cheap trick, BindToClose() its powerful and can be used in a proper way to prevent data lost when server closes in real roblox servers.
Do I place this just at the bottom of the leave function? Or before the pcall function?
Like this:
game.Players.PlayerAdded:Connect(function(player)
local newFolder = script:WaitForChild("PlayerData"):Clone()
newFolder.Parent = player
local savedValues = DataStore:GetAsync("FolderValues") or "New"
if savedValues == "New" then
print(player.Name.." has started the aventure!")
else
print(savedValues)
for ValName, Val in pairs(savedValues) do
if newFolder:FindFirstChild(ValName) then
newFolder:FindFirstChild(ValName).Value = Val
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local folder = player.PlayerData
local values = {}
for _, Val in pairs(player:FindFirstChild("PlayerData"):GetChildren()) do
--print(Val.Name, Val.Value)
values[Val.Name] = Val.Value
end
warn(values)
local succ, err = pcall(function()
return DataStore:SetAsync("FolderValues", values)
end)
if succ then
warn("Data saved!")
else
warn("failed to save", err)
end
warn("end of script") -- if this doesnt print means that game is closing before saving
end)
-- it doesnt really matter where in the script, but makes sense
game:BindToClose(function()
task.wait(5)
end)
Seems to be working, so all that does is prolongs the closing of studio/server? I noticed a longer wait when stopping the studio test.
Do lots of people run into this same issue? Cause this game isn’t that big, nor do I do any other leave functions or any other data saving besides this 1 script.
Well, along with the ideas I had why you are experiencing wrong values when joined and reading datastore, is having more scripts that edits the values in player, or when playing or when leaving, but warning the table shows that is fine.
So yes, should be that your game is shutting down fast, and studio doesnt have time to save. In real servers usually that wont happen, cause a player leaves and server still online cause theres more players, so server has enough time to save. And when last player leaves, supposedly server will still online 30 more seconds, specially if you use the BindToClose() it will try to perform all tasks you defined to do until 30 seconds, after that server will close not matter what, datastores saved or not.
Very cool, thank you sir.
One more thing, how should I go about accounting for the data store not loading yet?
Like if the player joins and leaves right away and it saves his null stats before they loaded?
About not overwritting a nil data. Its about make sure it did load firstly.
What I do is not letting player to start playing if Im not sure if it has data or not.
- If I get a signal that the connection with datastore is fine, and player has no data, then Im sure its a new player and set starterData for it.
- If I get a signal that connection with DS is fine, and player has data, then Im sure its an old player, and update its values.
- If I get a signal that connection FAILED with DS, I do retries until getting a right connection, if never happens after some repeats, better to warn player and kick them. Or warn that there is a Roblox Issue with DataBases and any actions in game will not be saved until Roblox DB is online again. So disable the saving function when that happens in game.
In that way you will never overwrite wrong data for the player. Well still depends if you are handling the data in game correctly, server sided, reading the proper data, not double editing at the same event in different scripts, etc, but thats a different topic.
Oh I forgot
I create a table in a server script, that gets a “true” for that player as a key in table, when Player is leaving, check if that table>key of player is true, if not, do not save.
The key will be true once the datastore is correctly loaded, and all values in player were updated, otherwise still false. So if player joins and leaves right away, table says the player is false or nil to be saved, so save function doesnt run for the player, so its not overwritting datastore with nil
I’m a little confused.
What do you mean by a table and key being true. Not sure what you mean?
Sorry, its just Im more used to use tables in scripts/modules instead of using values.
You can achieve the same by using values, what I meant is:
Player joins > Read Datastore > If datastore went fine > Create a value in player or a key in a table that states player loaded correctly and all its values are updated
Then
Player leaves > check if the Loaded value or key in table is true > If not true, saving wont happen, if true proceed to save
Oh I see, basically just a flag check. Okay thanks for all the help!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.