Leaderboard stats not working

Hey!

I currently working on a game and I have a DataStore script that creates the leaderstats saves them.
In the middle of the script there is a piece of code that is supposed to run when the player kills another player they get their stats.


I edited the code to my preference, unfortunately, the code does not work.
(Credits to @JackscarIitt for the original code).

The script:

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

local function CreateStat(name, class, value, parent)
	local stat = Instance.new(class)
	stat.Name = name
	stat.Value = value
	stat.Parent = parent
	
	return stat
end

local function GetKey(player)
	return player.UserId .. "-"
end

local function SaveData(player)
	local key = GetKey(player)
	
	local leaderstats = player.leaderstats
	local timeAliveData = leaderstats["Time"].Value
	local bestTimeData = leaderstats["TopTime"].Value
	local kills = leaderstats["Kills"].Value
	
	local success, result = pcall(function()
		DataStore:SetAsync(key .. "Time", timeAliveData)
		DataStore:SetAsync(key .. "TopTime", bestTimeData)
		DataStore:SetAsync(key .. "Kills", kills)
	end)
	
	if not success then
		warn(result)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local key = GetKey(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local timeAlive = CreateStat("Time", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("TopTime", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("Kills", "IntValue", 0, leaderstats)
	
	local timeAliveData, bestTimeData, kills
	
	local success, result = pcall(function()
		timeAliveData = DataStore:GetAsync(key .. "Time")
		bestTimeData = DataStore:GetAsync(key .. "TopTime")
		kills = DataStore:GetAsync(key .. "Kills")
		
		game.Players.PlayerAdded:Connect(function(player) --Code that does not work
			print("Player Added")
			player.CharacterAdded:Connect(function(Chr)
				print("Character added")
				local Humanoid = Chr:WaitForChild("Humanoid")
				print(player.Name.." has died!")

				Humanoid.Died:Connect(function()
					local CreatorTag = Humanoid:FindFirstChild("creator")

					if CreatorTag then
						local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
						timeAliveData += Leaderstats["Time Alive"].Value
						bestTimeData = timeAliveData
						Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

						print("Stole time!")
					else
						warn("Something else happened?") 
					end
				end)
			end)
		end)
		
	end) -- Ends here
	
	if success then
		timeAlive.Value = timeAliveData or 0
		bestTime.Value = bestTimeData or 0
		bestTime.Value = kills or 0
	else
		warn(result)
		print ("didnt save")
	end
end)

game.Players.PlayerRemoving:Connect(SaveData)

if not RunService:IsStudio() then
	game:BindToClose(function()
		if #game.Players:GetPlayers() <= 1 then return end
		
		for _, player in pairs(game.Players:GetPlayers()) do
			SaveData(player)
		end
	end)
end

This is the code found in the middle of the script that won’t work.

game.Players.PlayerAdded:Connect(function(player) --Code that does not work
			print("Player Added")
			player.CharacterAdded:Connect(function(Chr)
				print("Character added")
				local Humanoid = Chr:WaitForChild("Humanoid")
				print(player.Name.." has died!")

				Humanoid.Died:Connect(function()
					local CreatorTag = Humanoid:FindFirstChild("creator")

					if CreatorTag then
						local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
						timeAliveData += Leaderstats["Time Alive"].Value
						bestTimeData = timeAliveData
						Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

						print("Stole time!")
					else
						warn("Something else happened?") 
					end
				end)
			end)
		end)
		
	end) -- Ends here

More detailed example of what am trying to achieve:
Screen capture - f8ce0224e1a7e9a0028da277b4811a81 - Gyazo

Help is appreciated! :happy3:

Alright one issue: You’re encasing 2 PlayerAdded events inside of each other which is why the second script isn’t working since the first one has already fired, and went ahead to create those stats & GetAsync functions (Also why are you encasing it all in a pcall? It’s not necessary

You don’t need to add any more PlayerAdded events, 1 is fine

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

local function CreateStat(name, class, value, parent)
	local stat = Instance.new(class)
	stat.Name = name
	stat.Value = value
	stat.Parent = parent

	return stat
end

local function GetKey(player)
	return player.UserId .. "-"
end

local function SaveData(player)
	local key = GetKey(player)

	local leaderstats = player.leaderstats
	local timeAliveData = leaderstats["Time"].Value
	local bestTimeData = leaderstats["TopTime"].Value
	local kills = leaderstats["Kills"].Value

	local success, result = pcall(function()
		DataStore:SetAsync(key .. "Time", timeAliveData)
		DataStore:SetAsync(key .. "TopTime", bestTimeData)
		DataStore:SetAsync(key .. "Kills", kills)
	end)

	if not success then
		warn(result)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local key = GetKey(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local timeAlive = CreateStat("Time", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("TopTime", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("Kills", "IntValue", 0, leaderstats)

	local timeAliveData, bestTimeData, kills

	local success, result = pcall(function()
		timeAliveData = DataStore:GetAsync(key .. "Time")
		bestTimeData = DataStore:GetAsync(key .. "TopTime")
		kills = DataStore:GetAsync(key .. "Kills")
	end)
	
	if success then
		timeAlive.Value = timeAliveData or 0
		bestTime.Value = bestTimeData or 0
		bestTime.Value = kills or 0
	else
		warn(result)
		print ("didnt save")
	end
	
	print("Player Added")
	player.CharacterAdded:Connect(function(Chr)
		print("Character added")
		local Humanoid = Chr:WaitForChild("Humanoid")
		print(player.Name.." has died!")

		Humanoid.Died:Connect(function()
			local CreatorTag = Humanoid:FindFirstChild("creator")

			if CreatorTag then
				local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
				timeAliveData += Leaderstats["Time Alive"].Value
				bestTimeData = timeAliveData
				Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

				print("Stole time!")
			else
				warn("Something else happened?") 
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(SaveData)

if not RunService:IsStudio() then
	game:BindToClose(function()
		if #game.Players:GetPlayers() <= 1 then return end
		
		for _, player in pairs(game.Players:GetPlayers()) do
			SaveData(player)
		end
	end)
end

Hey @JackscarIitt, I tried your code but, it didn’t work. :slightly_frowning_face:
I got no output errors, (I tested this in a 2 player local server).

Is the CharacterAdded function printing anything?

@JackscarIitt, I am aware that there are prints in the script but, when I test them in a 2 player local server the prints don’t appear at all in the output at all. :thinking:

Are you checking the prints on the server window? It should be able to print just fine if you check it from there and everything’s working fine? If not, try adding a print before you even start your script?

I check the prints in the server window. It printed: image

Though it printed that, it never gave the killer the victim’s stats.

Oh this is because the “Died” print is inside the CharacterAdded event

So breaking it down to these lines here:

	player.CharacterAdded:Connect(function(Chr)
		print("Character added")
		local Humanoid = Chr:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
		    print(player.Name.." has died!")
			local CreatorTag = Humanoid:FindFirstChild("creator")
            print(CreatorTag)

			if CreatorTag then
                print("Found a tag")
				local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
				timeAliveData += Leaderstats["Time Alive"].Value
				bestTimeData = timeAliveData
				Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

				print("Stole time!")
			else
				warn("Something else happened?") 
			end
		end)
	end)

Try this?

Hey @JackscarIitt, I tried your new code, It didn’t work.

I only got this print: image

Why is it that Humanoid objects are the most struggling thing to get? Are you getting any infinite yield warnings for attempting to find the Humanoid variable?

Hey @JackscarIitt, No, I am not getting any infinite yield warnings for attempting to find the Humanoid variable.

try using repeat wait until the humanoid has loaded or if humanoid ~= nil

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

local function CreateStat(name, class, value, parent)
	local stat = Instance.new(class)
	stat.Name = name
	stat.Value = value
	stat.Parent = parent

	return stat
end

local function GetKey(player)
	return player.UserId .. "-"
end

local function SaveData(player)
	local key = GetKey(player)

	local leaderstats = player.leaderstats
	local timeAliveData = leaderstats["Time"].Value
	local bestTimeData = leaderstats["TopTime"].Value
	local kills = leaderstats["Kills"].Value

	local success, result = pcall(function()
		DataStore:SetAsync(key .. "Time", timeAliveData)
		DataStore:SetAsync(key .. "TopTime", bestTimeData)
		DataStore:SetAsync(key .. "Kills", kills)
	end)

	if not success then
		warn(result)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local key = GetKey(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local timeAlive = CreateStat("Time", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("TopTime", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("Kills", "IntValue", 0, leaderstats)

	local timeAliveData, bestTimeData, kills

	local success, result = pcall(function()
		timeAliveData = DataStore:GetAsync(key .. "Time")
		bestTimeData = DataStore:GetAsync(key .. "TopTime")
		kills = DataStore:GetAsync(key .. "Kills")

		local Chr = player.Character or player.CharacterAdded:Wait()
		print("Character exists")

		repeat wait() until Chr:FindFirstChild("Humanoid")

		local Humanoid = Chr.Humanoid

		Humanoid.Died:Connect(function()
			print(player.Name.." has died!")
			local CreatorTag = Humanoid:FindFirstChild("creator")

			if CreatorTag then
				local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
				timeAliveData += Leaderstats["Time Alive"].Value
				bestTimeData = timeAliveData
				Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

				print("Stole time!")
			else
				warn("Something else happened?") 
			end


		end)


	end) -- Ends here

	if success then
		timeAlive.Value = timeAliveData or 0
		bestTime.Value = bestTimeData or 0
		bestTime.Value = kills or 0
	else
		warn(result)
		print ("didnt save")
	end
end)

game.Players.PlayerRemoving:Connect(SaveData)

if not RunService:IsStudio() then
	game:BindToClose(function()
		if #game.Players:GetPlayers() <= 1 then return end

		for _, player in pairs(game.Players:GetPlayers()) do
			SaveData(player)
		end
	end)
end

Hey @JayO_X, I tried your script, it didn’t work. :thinking:

I did get all the prints, overall the stealing script didn’t work as intended.

1 Like