Does this leaderstats code actually save the stats, or no?

Hi there.

I’m trying to make a kills leaderboard that saves (With Datastore), but I must ask, am I doing it right? I just mashed a bunch of tutorials I watched together to make this script.

local datastoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local playersService = game:GetService("Players")
local getDatastore = datastoreService:GetDataStore("Kills")	

playersService.PlayerAdded:Connect(function(getPlayer)
	local getLeaderstats = Instance.new("Folder")
	getLeaderstats.Name = "leaderstats"
	getLeaderstats.Parent = getPlayer
	
	local getKillsValue = Instance.new("StringValue")
	getKillsValue.Name = "Kills"
	getKillsValue.Value = getDatastore:GetAsync(getPlayer.UserId) or 0	
	getKillsValue.Parent = getLeaderstats
	
	getPlayer.CharacterAdded:Connect(function(getCharacter)
		local getHumanoid = getCharacter:FindFirstChild("Humanoid")
		
		getHumanoid.Died:Connect(function(getDied)
			local getTag = getHumanoid:FindFirstChild("creator")
			local getKiller = getTag.Value
			
			if getTag and getKiller then
				getKiller.leaderstats.Kills.Value += 1
			end
		end)
	end)
end)

playersService.PlayerRemoving:Connect(function(getPlayer)
	getDatastore:SetAsync(getPlayer.UserId, getPlayer:WaitForChild("leaderstats").Kills.Value)
end)

game:BindToClose(function()
	task.wait(3)
end)

make it a protected call so that in case that there is a error the script won’t stop working.

Where do I make the pcall? I’m not really familiar with leaderboards and Datastore stuff like this.

you need to wrap it arround GetAsync and SetAsync

pcall tutorial

Am I doing it right?

local datastoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local playersService = game:GetService("Players")
local getDatastore = datastoreService:GetDataStore("Kills")	

playersService.PlayerAdded:Connect(function(getPlayer)
	local getLeaderstats = Instance.new("Folder")
	getLeaderstats.Name = "leaderstats"
	getLeaderstats.Parent = getPlayer
	
	local getKillsValue = Instance.new("StringValue")
	getKillsValue.Name = "Kills"
	
	local callSuccess, callError = pcall(function()
		getKillsValue.Value = getDatastore:GetAsync(getPlayer.UserId) or 0	
	end)
	
	if not callSuccess then
		getPlayer:Kick("Datastore error: " .. callError .. " (Please rejoin)")
	end
	
	getKillsValue.Parent = getLeaderstats
	
	getPlayer.CharacterAdded:Connect(function(getCharacter)
		local getHumanoid = getCharacter:FindFirstChild("Humanoid")
		
		getHumanoid.Died:Connect(function(getDied)
			local getTag = getHumanoid:FindFirstChild("creator")
			local getKiller = getTag.Value
			
			if getTag and getKiller then
				getKiller.leaderstats.Kills.Value += 1
			end
		end)
	end)
end)

playersService.PlayerRemoving:Connect(function(getPlayer)
	local callSuccess, callError = pcall(function()
		getDatastore:SetAsync(getPlayer.UserId, getPlayer:WaitForChild("leaderstats").Kills.Value)
	end)
end)

game:BindToClose(function()
	task.wait(2)
end)
local datastoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local playersService = game:GetService("Players")
local getDatastore = datastoreService:GetDataStore("Kills")	

playersService.PlayerAdded:Connect(function(getPlayer)
	getPlayer.CharacterAdded:Connect(function(getCharacter)
		local getHumanoid = getCharacter:FindFirstChild("Humanoid")

		getHumanoid.Died:Connect(function(getDied)
			local getTag = getHumanoid:FindFirstChild("creator")
			local getKiller = getTag.Value

			if getTag and getKiller then
				getKiller.leaderstats.Kills.Value += 1
			end
		end)
	end)
	
	local getLeaderstats = Instance.new("Folder")
	getLeaderstats.Name = "leaderstats"
	getLeaderstats.Parent = getPlayer

	local getKillsValue = Instance.new("StringValue")
	getKillsValue.Name = "Kills"
	getKillsValue.Parent = getLeaderstats
	
	local data
	local succ, err = pcall(function()
		data = getDatastore:GetAsync(getPlayer.UserId)
	end)
	
	if succ then
		if data then
			getKillsValue.Value = data
		end
	else
		warn(err)
	end
end)

playersService.PlayerRemoving:Connect(function(getPlayer)
	local data
	
	local succ, err = pcall(function()
		data = getDatastore:SetAsync(getPlayer.UserId, getPlayer.leaderstats.Kills.Value)
	end)
	
	if succ then
		if data then
			print(data)
		end
	else
		warn(err)
	end
end)

game:BindToClose(function()
	for _, getPlayer in ipairs(playersService:GetPlayers()) do
		local data

		local succ, err = pcall(function()
			data = getDatastore:SetAsync(getPlayer.UserId, getPlayer.leaderstats.Kills.Value)
		end)

		if succ then
			if data then
				print(data)
			end
		else
			warn(err)
		end
	end
end)

Version with pcall calls added. Just remember that “pcall” yields so it’s best to connect your “CharacterAdded” event before any DataStore loading is performed (as I have done in the above). Other than that your original script was fine.

1 Like

Alright, I’ll try this out. Thanks!

I joined in a private server with my friend, it works and saves fine but my friend doesn’t have any value? We both tried rejoining but he still doesn’t have anything.

Would this work?

local data
	local succ, err = pcall(function()
		data = getDatastore:GetAsync(getPlayer.UserId)
	end)

	if succ then
		if data then
			if data == nil then
				getKillsValue.Value = 0
			else
				getKillsValue.Value = data
			end
		end
	else
		warn(err)
	end

Oh, you have kills setup as a “StringValue”, change it to an “IntValue” instead. The default value for an “IntValue” instance is already 0.

Alright, I’ll try that and update you on what happens.

Alright, it works now. Thanks so much!