Saving Leaderstat

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I have three instances on the leaderstats Kills, Deaths, and XP. (Without breaking kills/deaths script) I only want to save the XP stats

  2. What is the issue? Include screenshots / videos if possible!
    Every time I attempt to try this, I end up saving all three instances which I don’t want or the leaderboard itself just stops working, I only want the XP stats value to save

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried looking through videos, and scrolling through other DevForums and there’s none I found that deals with saving on one specific stat on the leaderboard. Based on what I found on a video, I figured I’d try to put the scripts together and make them work somehow but no solution found so far

local Players = game:GetService('Players')

local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"
Instance.new('IntValue', Template).Name = "XP"

Players.PlayerAdded:Connect(function(Player:Player)
	task.wait(1)
	local Stats = Template:Clone()
	Stats.Parent = Player
	local Deaths = Stats.Deaths
	Player.CharacterAdded:Connect(function(Character)
		Deaths.Value = Deaths.Value + 1
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:Connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return
					end
				end
			end)
		end
	end)
end)


-----------------------------------------------------------------------------------

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-XP")
	end)

	if success then
		leaderstats.XP = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-XP",player.leaderstats.XP.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

Does the “Player data successfully saved!” ever print? Also, change this in your PlayerAdded function:

if success then
		leaderstats.XP.Value = data --You forgot to add .Value to the end of that
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end

It also seems like you are generating the leaderstats folder twice.

Updated Script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Kills = Instance.new("IntValue", leaderstats).Name = "Kills"
	local Deaths = Instance.new("IntValue", leaderstats).Name = "Deaths"
	local XP = Instance.new("IntValue", leaderstats).Name = "XP"

	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-XP")
	end)

	if success then
		XP.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end

Player.CharacterAdded:Connect(function(Character)
		Deaths.Value = Deaths.Value + 1
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:Connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild('leaderstats') and Killer.leaderstats:FindFirstChild("Kills") then
							Kills.Value += 1 --Same as doing Kills.Value = Kills.Value + 1
						end
						--return --I could be wrong but I think this return will completely stop the script.
					end
				end
			end)
		end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-XP", player.leaderstats.XP.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		warn("There was an error when saving data") --Basically the same thing as print() but has yellow text
		warn(errormessage)
	end
end)

game:BindToClose(function()
	task.wait(5) --Gives the server time to save.
end)

When I tried your updated script, the leaderstats doesn’t work anymore, I got some errors:

I got an error at Name =

	local Kills = Instance.new("IntValue", leaderstats).Name = "Kills"
	local Deaths = Instance.new("IntValue", leaderstats).Name = "Deaths"
	local XP = Instance.new("IntValue", leaderstats).Name = "XP"

and seems like there was an issue with calling the character as well, Player was not defined

	Player.CharacterAdded:Connect(function(Character)

at the end of the script, I get a red underline error under wait(5) and end)

		task.wait(5) --Gives the server time to save.
	end)

Try this

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Kills = Instance.new("IntValue", leaderstats)
	Kills.Name = "Kills"
	local Deaths = Instance.new("IntValue", leaderstats)
	Deaths.Name = "Deaths"
	local XP = Instance.new("IntValue", leaderstats)
	XP.Name = "XP"

	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-XP")
	end)

	if success then
		XP.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
	player.CharacterAdded:Connect(function(Character)
		Deaths.Value += 1
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:Connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild('leaderstats') and Killer.leaderstats:FindFirstChild("Kills") then
							Kills.Value += 1 --Same as doing Kills.Value = Kills.Value + 1
						end
						--return --I could be wrong but I think this return will completely stop the script.
					end
				end
			end)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-XP", player.leaderstats.XP.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		warn("There was an error when saving data") --Basically the same thing as print() but has yellow text
		warn(errormessage)
	end
end)

game:BindToClose(function()
	task.wait(5) --Gives the server time to save.
end)

I forgot to add an end) to the PlayerAdded event

1 Like

I tried it and it works perfectly, thank you so much!! :+1:

1 Like

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