Help with Leaderstats

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.

2 Likes

Add if statements under the pcalls

if not success then
print(errorMessage)
end
1 Like

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)
2 Likes

I just inserted this script and it doesn’t seem to be working. The leaderstats appear on the leaderboard but don’t save.

1 Like

Any errors?

1 Like

No, I can’t see any errors.

1 Like

Try this one:

--//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)
1 Like

Same thing is happening again.

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.

1 Like

Do not edit the value on the client, try to just edit on the server and test again.

1 Like

I have no idea what I did differently that time but it worked. I previously tried it on the server with no luck so that’s weird. Thanks for the help.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.