Hi, I’m a creator of game called Project X, and I’m using a script that saving values in my game, but today, all my values in game was resetted when I clicked on the X button when joining the game. How can I protect player from uninvited data reset?
A good data system shouldn’t have these issues happen (often). I say often because Roblox’s datastores aren’t the best but there are things you should do to reduce the chance of data corruption.
Auto saving data
Saving data when the player leaves
Saving data when the server crashes
Saving data when a big purchase is made
Using pcalls to check datastoreservice availability and using pcalls for getting data itself
What data store system are you using? Some are known to be more reliable than others
game.Players.PlayerRemoving:connect(function(player)
local datastore = game:GetService(“DataStoreService”):GetDataStore(player.Name…“Data”)
local statstorage = player:FindFirstChild(“Banned”):GetChildren()
for i = 1, #statstorage do
datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
end
end)
local player = game.Players.PlayerAdded:connect(function(player)
local datastore = game:GetService(“DataStoreService”):GetDataStore(player.Name…“Data”)
player:WaitForChild(“Banned”)
wait()
local stats = player:FindFirstChild(“Banned”):GetChildren()
for i = 1, #stats do
stats[i].Value = datastore:GetAsync(stats[i].Name)
end
end)
This one
It looks like you are requesting a lot of SetAsync() functions at once. Try compressing it into a single array. Also, as @AlteredMind said, use a pcall() whenever accessing the store.
I don’t know how to use pcall, there’s no any in my game
just checking because this is cause for 99% of errors with datastores, did you include a BindToClose() save function as well? this will save the players data if they are kicked, crashed, or if you as the dev are playtesting in studio.
Players.PlayerRemoving works 89% of the time, but it best to have a back up BindToClose function as well.
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
as well as:
game.Players.PlayerRemoving:Connect(function(player)
player:Destroy()
A pcall() is simple. It will return the provided function’s success and any results it gives. In this case, we’re using the result part to catch any errors.
For example:
local ds = game:GetService("DataStoreService")
local exampleStore = ds:GetDataStore("Example")
--then, when accessing the store:
local key = nil --whatever you use to access the DataStore
local data
local success, err = pcall(function()
data = exampleStore:GetAsync(key)
end)
if success then
print("No errors!")
elseif not success and err then
warn("Errors!")
--whatever you want to happen if it failed.
--An example could be calling the function to load data again.
end
I would suggset using UpdateAsync and using Pcalls, if you don’t know how to I highly highly suggset you take a look online into the subject because data is such an important thing
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.