Hello! As I was making a data saving system for my game, I found that the code that saves the data won’t work. Its gets up to the “Start Saving” print command and then fails with no errors.
Players.PlayerRemoving:Connect(function(player)
print("Data Saving...")
local success, errormessage = pcall(function()
print("Starting Save") -- Last functioning print
WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
print("wins saved")
WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
print("coins saved")
end)
if success then
print("Player Data Successfully Saved!")
else
print("Couldn't Save!")
warn(errormessage)
end
end)
I dont get why you dont save them together, doing this will give the player two keys inside of just having one, having one key is much more efficient as you dont have to send multiple requests to get a single fragment of data, instead you can simply send one request to get all the Data.
Also, you should probably remove the print functions inside the pcall and see if that helps, just ensure that the Save was a Success and then use print to check if a specifc condition occurred.
I’m not sure what’s wrong with your script, but give this one a shot & let me know if it works.
Players.PlayerRemoving:Connect(function(player)
print("Data Saving...")
local success, errormessage = pcall(function()
print("Starting Save")
local winsSaved = WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
if not winsSaved then
error("Failed to save wins for player "..player.Name)
else
print("wins saved")
end
local coinsSaved = WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
if not coinsSaved then
error("Failed to save coins for player "..player.Name)
else
print("coins saved")
end
end)
if success then
print("Player Data Successfully Saved!")
else
print("Couldn't Save!")
warn(errormessage)
end
end)
After testing the code, I think the problem is that the game is closing before it can save so can you try to add a game:BindToClose() function with the parameter being wait to well wait before the game closes to give the datastore time to save.
That’s definitely not the case as PlayerRemoving will sometimes fire even when player disconnects because sometimes the player will get kicked for connection loss when you stop playtesting.
Well, I tried it in studio and it worked for me, and I also noticed that sometimes the server immediately closes due to there being no loading when you stop play testing…
Keep in mind that the usage of BindToClose in DataStoreService is to ensure Data saves, the Studio Server is different from a Real Game Server, on Real Servers, there will be a 30 second wait for the Data and other stuff like BindToClose to properly run. On Studio there is no wait like this, so as soon as you leave, it will close immediately, BindToClose will run briefly.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local WinsData = DataStoreService:GetDataStore("WinsData")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Value = 0
Wins.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
local data, data2
local success, errormessage = pcall(function()
data = WinsData:GetAsync(player.UserId.."-wins")
data2 = WinsData:GetAsync(player.UserId.."-coins")
end)
if success then
print("Data Loaded!")
Wins.Value = data
Coins.Value = data2
else
print("Couldn't load Data!")
warn(errormessage)
end
end
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(function(player)
print("Data Saving...")
local success, errormessage = pcall(function()
print("Starting Save")
local winsSaved = WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
if not winsSaved then
error("Failed to save wins for player "..player.Name)
else
print("wins saved")
end
local coinsSaved = WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
if not coinsSaved then
error("Failed to save coins for player "..player.Name)
else
print("coins saved")
end
end)
if success then
print("Player Data Successfully Saved!")
else
print("Couldn't Save!")
warn(errormessage)
end
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local WinsData = DataStoreService:GetDataStore("WinsData")
game:BindToClose(wait)
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Value = 0
Wins.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
local data, data2
local success, errormessage = pcall(function()
data = WinsData:GetAsync(player.UserId.."-wins")
data2 = WinsData:GetAsync(player.UserId.."-coins")
end)
if success then
print("Data Loaded!")
Wins.Value = data
Coins.Value = data2
else
print("Couldn't load Data!")
warn(errormessage)
end
end
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(function(player)
print("Data Saving...")
local success, errormessage = pcall(function()
print("Starting Save")
local winsSaved = WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
if not winsSaved then
error("Failed to save wins for player "..player.Name)
else
print("wins saved")
end
local coinsSaved = WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
if not coinsSaved then
error("Failed to save coins for player "..player.Name)
else
print("coins saved")
end
end)
if success then
print("Player Data Successfully Saved!")
else
print("Couldn't Save!")
warn(errormessage)
end
end)