Saving Data Problem

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)
1 Like

Can you please put all your code into a code block and format it properly so it’s easily readable, thanks.

Also you copy and pasted your post in the same post.

Try individualising :SetAsync and :GetAsync to have one Async for each pcall.

Just tried using this still the same thing happens


> pcall(function()
>  Cookies.Value = ds1:GetAsync(plr.UserId) or 0
> end)
> pcall(function()
> 
> 
>  ds1:SetAsync(plr.UserId, Cookies.Value)
> end) 
>  pcall(function()
> 
> 
>    CPS.Value = CPSs1:GetAsync(plr.UserId) or 0 --------wont work 
> end)
> pcall(function()
> 
> 
>  	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 
> end)
> pcall(function()
> 
> 
>  ds1:SetAsync(plr.UserId, Cookies.Value)
> 	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)
1 Like

could this be the problem I’m adding the value of cps trough a local script meaning its not server sided.??

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.

thank you all I have to do it make it fire a remote event thanks again

1 Like

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