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)
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.
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)
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})