I have a leaderboard that is fully functional. all I want is so that when a player leaves the game the data save. The cookie’s value saves but the Cps value won’t and it’s written the same way. Any solution I have no idea. Also, I get no errors.
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CookieSaveSystem")
local CPSs1 = datastore:GetDataStore("CPSSaveSystem")
local playerleft = 0
local savedoubounce = 0 game.Players.PlayerAdded:connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local Cookies = Instance.new("IntValue", folder)
Cookies.Name = "Cookies"
local CPS = Instance.new("IntValue", folder)
CPS.Name = "CPS"
local SaveFolder = Instance.new("Folder",plr)
SaveFolder.Name = "SaveFolder"
local WitchPlot = Instance.new("StringValue",plr)
WitchPlot.Name = "WitchPlot"
local HasGotGrandma1 = Instance.new("BoolValue",SaveFolder)
HasGotGrandma1.Name = "HasGotGrandma1"
local playerownsAPlot = Instance.new("BoolValue",plr)
playerownsAPlot.Name = "playerownsAPlot"
pcall(function()
Cookies.Value = ds1:GetAsync(plr.UserId) or 0
ds1:SetAsync(plr.UserId, Cookies.Value)
CPS.Value = CPSs1:GetAsync(plr.UserId) or 0 --------wont work
CPSs1:SetAsync(plr.UserId, CPS.Value) ---------wont work
end)
game.Players.PlayerRemoving:Connect(function(plr)
pcall(function()
CPSs1:SetAsync(plr.UserId, CPS.Value) ------wont work
ds1:SetAsync(plr.UserId, Cookies.Value)
end)
print(Cookies.Value) --------- prints the Cookies Saved Value
print(CPS.Value) ------- prints 0
end)
end)
Well, you can just do two separate functions for the player joining and player leaving. Here is the code with the two separate functions instead of two combined with pcall that worked for me. if it does not work in studio, play in-game and rejoin to check.
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CookieSaveSystem")
local CPSs1 = datastore:GetDataStore("CPSSaveSystem")
local playerleft = 0
local savedoubounce = 0
game.Players.PlayerAdded:connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local Cookies = Instance.new("IntValue", folder)
Cookies.Name = "Cookies"
local CPS = Instance.new("IntValue", folder)
CPS.Name = "CPS"
local SaveFolder = Instance.new("Folder",plr)
SaveFolder.Name = "SaveFolder"
local WitchPlot = Instance.new("StringValue",plr)
WitchPlot.Name = "WitchPlot"
local HasGotGrandma1 = Instance.new("BoolValue",SaveFolder)
HasGotGrandma1.Name = "HasGotGrandma1"
local playerownsAPlot = Instance.new("BoolValue",plr)
playerownsAPlot.Name = "playerownsAPlot"
Cookies.Value = ds1:GetAsync(plr.UserId) or 0
ds1:SetAsync(plr.UserId, Cookies.Value)
CPS.Value = CPSs1:GetAsync(plr.UserId) or 0
CPSs1:SetAsync(plr.UserId, CPS.Value)
end)
game.Players.PlayerRemoving:Connect(function(plr)
CPSs1:SetAsync(plr.UserId, plr.leaderstats.CPS.Value)
ds1:SetAsync(plr.UserId, plr.leaderstats.Cookies.Value)
end)
Yes, it can create issues since it’s change won’t be recognized. Try using a RemoteEvent, and add a script in ServerScriptService to add a value to the leaderstats. Also for the script I gave, if it’s not already, put it in a script in ServerScriptService.
I believe its good practice to do one Async for one Pcall because you can then know which what Async failed to get or save data and try to deal with the problem.
Those pcall’s aren’t being used efficiently either, here’s how you can efficiently use a pcall
local Success, DataOrError = pcall(CPSs1.GetAsync, CPSs1, plr.UserId)
if Success then
-- DataOrError is the data returned
else
-- DataOrError is the error message
end