local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", leaderstats)
Coins.Name = "Coins"
local Gems = Instance.new("IntValue", leaderstats)
Gems.Name = "Gems"
local Rebirths = Instance.new("IntValue", leaderstats)
Rebirths.Name = "Rebirths"
local playerUserID = "Player_"..player.UserId
-- Load Data
local data
local success, errormessage = pcall(function()
DataStore:GetAsync(playerUserID)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserID = "Player_"..player.UserId
local data = {
Coins = player.leaderstats.Coins.Value;
Gems = player.leaderstats.Gems.Value;
Rebirths = player.leaderstats.Rebirths.Value;
}
local success, errormessage = pcall(function()
DataStore:SetAsync(playerUserID, data)
end)
if success then
print("Successfully Saved!")
else
print("There was an error!")
warn(errormessage)
end
end)
But there is no data variable in the function. Look:
game.Players.PlayerRemoving:Connect(function(player)
local playerUserID = "Player_"..player.UserId
local data = game.HttpService:JSONEncode(data)
local success, errormessage = pcall(function()
DataStore:SetAsync(playerUserID, data)
end)
if success then
print("Successfully Saved!")
else
print("There was an error!")
warn(errormessage)
end
end)
âSetAsyncâ not accept tables with value, JSONEncode transforms the table in a string, and JSONDecode transforms that string in an array again. You need modify now the âGetAsyncâ function, because the second value returned of âGetAsyncâ function is not a error message. You can do this with your code: