Help with saving leaderstats

Hey, so I have a code that has 2 leader stats, size, and coins. Size saves and loads correctly however Coins don’t save but do a load.

Here is the code:

local DataStoreService = game:GetService(“DataStoreService”)
local SIZE = game.StarterGui.UI.SIZE
local VSIZE = game.StarterGui.UI.VSIZE
local CAPACITY = game.StarterGui.UI.CAPACITY
local VCOIN = game.StarterGui.UI.VCOIN
local COIN = game.StarterGui.UI.COINS
local BOOK = game.StarterGui.UI.BOOK
local IQ = game.StarterGui.UI.IQ
local playerCount = #game.Players:GetPlayers()

game.Players.PlayerAdded:Connect(function(Player)
local CheetahData = DataStoreService:GetDataStore(“Cheetah”…Player.UserId)
local PlayerId = “Id-”…Player.UserId
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = Player

local Value = Instance.new("IntValue")
Value.Name = "Size"
Value.Parent = leaderstats
Value.Value = 0

local Value2 = Instance.new("IntValue")
Value2.Name = "Coins"
Value2.Parent = leaderstats
Value2.Value = 0

local GetSaved = CheetahData:GetAsync(PlayerId)
local Saved,Error = pcall(function()
	Value.Value = GetSaved[1]
	VSIZE.Value = GetSaved[1]
	Value2.Value = GetSaved[2]
	VCOIN.Value = GetSaved[2]
end)
if not Saved then
	print(tostring(Error))
	local Saving2 = {Value2.Value}
	local Saving1 = {Value.Value}
	CheetahData:SetAsync(PlayerId,Saving1,Saving2)
end

SIZE.Text = VSIZE.value.." | "..CAPACITY.Value		

game.ReplicatedStorage.HEADINCREASESTAT.OnServerEvent:Connect(function(player)
	local playercounts = playerCount
	print(playercounts)
	player.leaderstats.Size.Value = player.leaderstats.Size.Value + 1
	--Value.Value = Value.Value - playerCount
--	print(playerCount)
end)

game.ReplicatedStorage.SELLSTAT.OnServerEvent:Connect(function(player)
	local playercounts = playerCount
	print("SELLSTAT")
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + player.leaderstats.Size.Value * IQ.Value
	player.leaderstats.Size.Value = 0
	wait(1)
	--Value.Value = Value.Value - playerCount
--	print(playerCount)
end)

end)

game.Players.PlayerRemoving:Connect(function(Player)
local CheetahData = DataStoreService:GetDataStore(“Cheetah”…Player.UserId)
local PlayerId = “Id-”…Player.UserId
local Save1 = {Player.leaderstats.Size.Value}
local Save2 = (Player.leaderstats.Coins.Value)
CheetahData:SetAsync(PlayerId,Save1,Save2)
end)

What is the problem with my code?
Thanks :slight_smile:

1 Like

Try wrapping your :SetAsync() function in a pcall

Since :SetAsync() tends to fail when executed incorrectly, we must make sure that you are not overloading the request throttle.

game.Players.PlayerRemoving:Connect(function(Player)
   local CheetahData = DataStoreService:GetDataStore(“Cheetah”…Player.UserId)
   local PlayerId = “Id-”…Player.UserId
   local Save1 = {Player.leaderstats.Size.Value}
   local Save2 = (Player.leaderstats.Coins.Value)
   local Success, err = pcall(function()
      CheetahData:SetAsync(PlayerId,Save1,Save2)
   end)
   if Success then print("Saved") elseif err then error(err) end
end)

Now run it and show us what the output looks like.

2 Likes

Also, it is really ineffective to use :GetDataStore() with a PlayerAdded and PlayerRemoving events.

Try moving the CheetahData to the variables.

2 Likes

Hey thanks for trying to help, the code however still doesn’t work.

1 Like

Can you provide your output? There are many problems with the system and I recommend you rewrite it :slight_smile:

1 Like

There is no output it just loads the size but not coins.

1 Like
 local Success, err = pcall(function()
      CheetahData:SetAsync(PlayerId,Save1,Save2)
   end)

This is where the problem is. :SetAsync() only requires 2 parameters. If you wish to save 2 pieces of data, put the data in an array or you can use 2 :SetAsync() functions but that requires more memory consumption.

Try this:

game.Players.PlayerRemoving:Connect(function(Player)
   local CheetahData = DataStoreService:GetDataStore(“Cheetah”…Player.UserId)
   local PlayerId = “Id-”…Player.UserId
   local Save1 = {Player.leaderstats.Size.Value}
   local Save2 = (Player.leaderstats.Coins.Value)
   local Success, err = pcall(function()
      CheetahData:SetAsync(PlayerId, {Save1,Save2})
   end)
   if Success then print("Saved") elseif err then error(err) end
end)
1 Like

Now its not saving or loading size,I think ill just change my whole system then.

2 Likes

dont really know how devforum works but you should change your datastore to a dictionary, like
:SetAsync(PlayerId, {[“Coins”] = Player.leaderstats.Coins.Value, [“Size”] = Player.leaderstats.SizeValue})

1 Like

if you’d like, i can give you my leaderstats script?

1 Like

the script is

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Size= Instance.new("IntValue")
Size.Name = "Size"
Size.Value = 0
Size.Parent = leaderstats
local Coins= Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats

local Data = DataStore:GetAsync(Player.UserId)
if Data then
	Size.Value = Data.Size
	Coins.Value = Data.Coins

	else
	print("There Was An Error Saving"..Player.UserId " 's Data :(")
end
end)

game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Size"] = Player.leaderstats.Size.Value;
["Coins"] = Player.leaderstats.Coins.Value;

	})
	


end)

i just dont think i can help with the other stuff, sorry… :grimacing: