local ds = game:GetService("DataStoreService"):GetDataStore("GoldStorage")
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder",plr)
stats.Name = "leaderstats"
local coins = Instance.new("IntValue",stats)
coins.Name = "Gold"
coins.Value = ds:GetAsync(plr.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(plr)
wait(1)
local stats = plr.leaderstats
local gold = plr.Gold
ds:SetAsync(plr.UserId,gold.Value)
end)
This my my script to save gold into the place I am using, I did a lot of research in the devforum for answers but it either was a different way that was too confusing, or was just generally confusing.
Can someone help me understand what I did wrong? I’m testing it IN-GAME and it still doesn’t work from when I join to leave.
PlayerRemoving fires just before the player leaves, which is leaving you with very little time to save your data, on top of that you are waiting one additional second, please read this article about game:BindToClose(). I also recommend instead of using SetAsync() that you use UpdateAsync(), as it just updates the player data instead of setting the player’s data each time.
Also please make sure to use pcall, just so the script continues despite if an error gets thrown or not when loading or saving player’s data.
There is no reason to yield the thread, and Gold should be a member of leaderstats, not plr.
game.Players.PlayerRemoving:Connect(function(plr)
wait(1)
local stats = plr.leaderstats
local gold = plr.Gold
ds:SetAsync(plr.UserId,gold.Value)
end)
Another main reason is that if you’re testing this on Studio, the server shuts down when you leave as you’re the only player, therefore you also need to save data on server shutdown via BindToClose.
local stats = Instance.new("Folder",plr)
stats.Name = "leaderstats"
local coins = Instance.new("IntValue",stats)
coins.Name = "Gold"
coins.Value = ds:GetAsync(plr.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(plr)
local stats = plr.leaderstats
local gold = stats.Gold
ds:SetAsync(plr.UserId, gold.Value)
end)
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
coroutine.wrap(ds.SetAsync)(ds, player.UserId, player.leaderstats.Gold.Value)
end
end)
That tutorial is very confusing, I’ve tried using it but I just didn’t understand it. Like it works, but I have no idea how it works except for some portions. I like the way I am doing it, and it’s worked before this way like a week or two ago, but then it just stopped working.
In fact, I never understood it either. roblox should explain itself better with its tutorials. Try searching for datastores in this forum, maybe you can find some.
Yeah, but their new tutorials are pretty good, but this specific one is just extremely confusing. I searched the entire devforum and they either didn’t make sense or they weren’t my method.
local ds = game:GetService("DataStoreService"):GetDataStore("GoldStorage")
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder",plr)
stats.Name = "leaderstats"
local coins = Instance.new("IntValue",stats)
coins.Name = "Gold"
coins.Value = ds:GetAsync(plr.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(plr)
wait(1)
local stats = plr.leaderstats
local gold = stats.Gold -- You put plr.Gold before
ds:SetAsync(plr.UserId,gold.Value)
end)