Also I think there will be a rare occurrence of when PlayerRemoving event and BindToClose will run at the same time. But even if it does, I think it will work just fine.
Yea, it will just give a warning but the data will still be saved
Hey,
Your data seems to be too large.
To make the size of the data smaller, encode and decode it with HttpService.
Also, store them in tables.
Just search up how to encode and save data in tables in DevForum, no need to make this reply too long.
Is the data too big so it doesnât get saved?
I tested this and it works well.
I donât think a single coin value is that large
Ahh! How couldnât I understand!
Here,
-- Studio Saving
game:BindToClose(function()
for i, player: Player in pairs(game.Players:GetPlayers()) do
local __data = {
COINS = player.leaderstats.Coins.Value
-- Add more as needed
}
pcall(function()
yourdatastore:SetAsync(player.UserId, game:GetService("HttpService"):JSONEncode(__data)
end)
end
end)
-- Loading data
game.Players.PlayerAdded:Connect(function(player: Player)
-- Create leaderstats values here
local success, result = pcall(function()
return yourdatastore:GetAsync(player.UserId)
end
if success and result then
local decodedData = game:GetService("HttpService"):JSONDecode(result)
local coinsValue = decodedData.COINS
-- Set the leaderstats coin value
yourleaderstatscoinvalue.Value = tonumber(coinsValue)
end
end)
-- Normal Game Saving
game.Players.PlayerRemoving:Connect(function(player: Player)
local __data = {
COINS = player.leaderstats.Coins.Value
-- Add more as needed
}
pcall(function()
yourdatastore:SetAsync(player.UserId, game:GetService("HttpService"):JSONEncode(__data)
end)
end)
Players.PlayerRemoving may not save perfectly in Studio since you may hit the other stop button to stop the session. Thatâs why we use BindToClose.
Why did you use HttpService
for this?
HttpService helps us encode and decode data into strings, which lowers the size of the data.
Maybe mark as the solution ?
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.