Title explains it. I have 6 leaderboards and 1 data store script
Here is the data store script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore5")
local RunService = game:GetService("RunService")
serverStorage = game.ServerStorage
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local Strength = Instance.new("NumberValue")
Strength.Name = "Strength"
Strength.Parent = leaderstats
if success then
Strength.Value = data.Strength
end
local success, errormessage = pcall(function()
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local PlayerUserId = "Player_"..plr.UserId
local data = {
Strength = plr.leaderstats.Strength.Value;
}
local success, errormessage = pcall(function()
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
return
end
for _,plr in pairs(game.Players:GetPlayers()) do
local PlayerUserId = "Player_"..plr.UserId
local data = {
Strength = plr.leaderstats.Strength.Value;
}
local success, errormessage = pcall(function()
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end
end)
Auto save local script:
while wait(300) do
game.ReplicatedStorage.AutoSave:FireServer(game.Players.LocalPlayer)
script.Parent.Parent.PlayerGui.AutoSave.Enabled = true
wait(5)
script.Parent.Parent.PlayerGui.AutoSave.Enabled = false
end
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore3")
game.ReplicatedStorage.AutoSave.OnServerEvent:Connect(function(plr)
local PlayerUserId = "Player_"..plr.UserId
local data = {
Strength = plr.leaderstats.Strength.Value;
}
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
Please help me fix this problem since my game had 10 playings and i had to private it just for those reasons
It’s a very in depth tutorial but you don’t have to do everything with it. It shows you how to make a datastore more secure as well as show you how to use basically everything about datastores. I’d highly recommend reading it as at least for me, it helped me a lot.
Uh, so, something that I notice is that if the data doesn’t successfully gather the data, it will not write any data, and when the player leaves, there is a chance that it will overwrite the old data with the blank new one. My recommendation is that you do not save the data if they player is leaving the game and the data wasn’t loaded successfully in the first place. Also, I recommend you “retry” to get the data a couple of times. If that doesn’t work, you can kick the player and prompt them to rejoin. I’ve seen a couple of games follow this approach. Additionally, you could look into DataStore2 since it has retry features and backup features if I am correct. I apologize for the short and rushed reply, as well as any grammatical mistakes, I am from my phone, so apologies. Hope I helped.
My problem is still not fixed. I have tried game:BindToClose() and a random player lost their stats because of the amount of data store requests (since it fires everytime a player leaves) and i have tried auto data saving but it still didn’t work
And i cannot use DataStore2 since its going to reset my stats.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore5")
local RunService = game:GetService("RunService")
serverStorage = game.ServerStorage
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local Strength = Instance.new("NumberValue")
Strength.Name = "Strength"
Strength.Parent = leaderstats
if success then
Strength.Value = data.Strength
end
local success, errormessage = pcall(function()
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local PlayerUserId = "Player_"..plr.UserId
local data = {
Strength = plr.leaderstats.Strength.Value;
}
local success, errormessage = pcall(function()
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
return
end
for _,plr in pairs(game.Players:GetPlayers()) do
local PlayerUserId = "Player_"..plr.UserId
local data = {
Strength = plr.leaderstats.Strength.Value;
}
local success, errormessage = pcall(function()
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end
end)```
Wrap this in a coroutine so it saves all the players’ data at once.
Also, I don’t see any autosaving?
Edit: How to use corountines:
if RunService:IsStudio() then
return
end
for _,plr in pairs(game.Players:GetPlayers()) do
corountine.wrap(function()
local PlayerUserId = "Player_"..plr.UserId
local data = {
Strength = plr.leaderstats.Strength.Value;
}
local success, errormessage = pcall(function()
DataStore:UpdateAsync(PlayerUserId, function(OldData)
return data
end)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end)()
end