Hello. I’m working on a game and I am adding coins. I followed a leaderstats tutorial and wrote this script:
local dss = game:GetService("DataStoreService")
local coindatastore = dss:GetDataStore("coins")
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 PlayerID = "player_"..player.UserId
local coindata
local success, errormessage = pcall(function()
coindata = coindatastore:GetAsync(PlayerID)
end)
if success then
coins.Value = coindata
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerID = "player_"..player.UserId
local coinvalue = player.leaderstats.Coins.Value
local success, errormessage = pcall(function()
coindatastore:SetAsync(PlayerID,coinvalue)
end)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
local PlayerID = "Player_"..player.UserId
local coinvalue = player.leaderstats.Coins.Value
local success, errormessage = pcall(function()
coindatastore:SetAsync(PlayerID,coinvalue)
end)
end
end)
The amount of coins I have isn’t saving when I leave the game. Any idea why? Thanks.
It’s because in your game:BindToClose function, you accidentally capitalized Player.
Fixed code: (I would recommend just using the user id as their key instead of Player_ added to the front)
--//Services
local Players = game:GetService("Players")
local dss = game:GetService("DataStoreService")
--//Variables
local coindatastore = dss:GetDataStore("coins")
--//Functions
local function OnPlayerRemoving(player)
local success, errorMessage = pcall(function()
coindatastore:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
if not success then
warn(errorMessage)
end
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
local coindata = nil
local success, errorMessage = pcall(function()
coindata = coindatastore:GetAsync(player.UserId)
end)
if success then
coins.Value = coindata
else
warn(errorMessage)
end
end)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(function()
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(OnPlayerRemoving, player)
end
end)
--//Services
local Players = game:GetService("Players")
local dss = game:GetService("DataStoreService")
--//Variables
local coindatastore = dss:GetDataStore("coins")
--//Functions
local function OnPlayerRemoving(player)
local success, errorMessage = pcall(function()
coindatastore:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
if not success then
warn(errorMessage)
end
end
Players.PlayerAdded:Connect(function(player)
local savedCoins = coindatastore:GetAsync(player.UserId) or 0
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = savedCoins
coins.Parent = leaderstats
end)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(function()
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(OnPlayerRemoving, player)
end
end)
It works fine for me, are you editing the values when testing properly? (if you’re editing the Coins value on the client it won’t replicate the change onto the server)
idk what’s going on. I’ve edited the value on the client and on the server but nothing seems to be working. I don’t think the script is the issue. I think I’m doing everything correctly. The script is in ServerScriptService.