So I’m trying to make a script which saves/loads the players data, however it does not appear to work, I’m unsure if it’s with saving or loading the data however.
NOTE: I’m moving on from this, there’s really no point in adding to this unless you want to help someone else out (or waste your time)
local DSS = game:GetService("DataStoreService")
local gold = DSS:GetDataStore("gold")
-- LOAD
game.Players.PlayerAdded:Connect(function(player)
print ("joining")
local folder = Instance.new("Folder", player)
folder.Name = "leaderstats"
-- GOLD AND UNBANKED GOLD
local Gold = Instance.new("IntValue", folder)
Gold.Name = "Gold"
Gold.Value = gold:GetAsync(player.UserId) or 0
end)
-- SAVING
game.Players.PlayerRemoving:Connect(function(player)
print ("leaving")
local success, err = pcall(function()
print ("Success")
local PlayerUserId = 'Player_'..player.UserId
local Gold = player.leaderstats.Gold
gold:SetAsync(player.UserId, Gold.Value)
end)
if not success then
warn('Could Not Save Data')
end
end)
I just tested your code. It worked for me. I think the problem not with loading or saving, but how you update the Gold. Can you provide a piece of code where you give/remove Gold?
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local gold = DSS:GetDataStore("gold")
-- LOAD
Players.PlayerAdded:Connect(function(player)
print ("joining")
local folder = Instance.new("Folder", player)
folder.Name = "leaderstats"
-- GOLD AND UNBANKED GOLD
local Gold = Instance.new("IntValue", folder)
Gold.Name = "Gold"
local success1, value = pcall(function()
return gold:GetAsync(player.UserId)
end)
if success1 and value ~= nil then
Gold.Value = gold:GetAsync(player.UserId) or 0
end
end)
-- SAVING
function Save(player)
print ("leaving")
local Gold = player.leaderstats.Gold
local success1, value = pcall(function()
return gold:GetAsync(player.UserId)
end)
if success1 and value ~= nil then
local success2, err = pcall(function()
gold:UpdateAsync(player.UserId,function()
return Gold.Value
end)
end)
if success2 then print("Saved!")
else warn('Could Not Save Data') end
elseif success1 and value == nil then
local success2, err = pcall(function()
gold:SetAsync(player.UserId, Gold.Value)
end)
if success2 then print("Saved!")
else warn('Could Not Save Data') end
elseif not success1 then warn("Error") end
end
Players.PlayerRemoving:Connect(function(player)
Save(player)
end)
game:BindToClose(function()
for i,player in pairs(Players:GetPlayers()) do
coroutine.wrap(Save)(player)
end
end)