Global Leaderboard/Leaderstats Doesn't Save After Rejoining

Hello!
I was trying to make a global leaderboard & leaderstats for kills. Everything works until the point where when you rejoin, (not leave, but join back after leaving) your kills on leaderstats & leaderboard resets. But if you just leave and not rejoin your kills stay the same on the leaderboard in any server. I’m not sure why.

I would provide a video but it says there was an error importing it.

The code for the Leaderstats;

game.Players.PlayerAdded:Connect(function(Plr) -- Fires when a new Player joins the server
	local Leaderstats = Instance.new("Folder",Plr) -- Adding a folder inside Player
	Leaderstats.Name = "leaderstats" -- Naming the folder to "leaderstats"

	local Kills = Instance.new("IntValue",Leaderstats) -- Adding a Integer Value inside leaderstats
	Kills.Name = "Kills" -- Renaming the value to "Kills"

	Plr.CharacterAdded:Connect(function(Char) -- Fires when player's character is added
		local Humanoid = Char:FindFirstChild("Humanoid") -- Find Humanoid in Player's Character
		if Humanoid then -- Checking if Humanoid Exist
			Humanoid.Died:Connect(function() -- Fires when Humanoid is Died aka Player died in game
				for i,obj in pairs(Humanoid:GetChildren()) do -- Loop through everything inside Humanoid (obj)
					if obj:IsA("ObjectValue") and obj.Value and obj.Value:IsA("Player") then -- Checking if obj is a Object Value and its Value is Player
						local Killer = obj.Value -- Getting Player who killed
						local Kills = Killer.leaderstats.Kills -- Getting Kills (Integer Value) in killer's leaderstats
						Kills.Value += 1 -- Adding 1 kill in kills Value
					end
				end
			end)
		end
	end)
end)

The code for the Leaderboard;

local ds = game:GetService("DataStoreService")

local KillsODS = ds:GetOrderedDataStore("KillsStats")


local timeUntilReset = 10


while wait(1) do
	
	
	timeUntilReset = timeUntilReset - 1
	
	script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
	
	
	if timeUntilReset == 0 then
		
		timeUntilReset = 10
	
	
		for i, plr in pairs(game.Players:GetPlayers()) do
			
			KillsODS:SetAsync(plr.UserId, plr.leaderstats.Kills.Value)
		end
		
		for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
			
			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end
		
		
		local success, errorMsg = pcall(function()
			
			local data = KillsODS:GetSortedAsync(false, 50)
			local KillsPage = data:GetCurrentPage()
			
			for rankInLB, dataStored in ipairs(KillsPage) do
				
				
				local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
				local Kills = dataStored.value
				
				
				local template = script.Template:Clone()
				
				template.Name = name .. "Leaderboard"
				
				template.PlrName.Text = name
				
				template.Rank.Text = "#" .. rankInLB
				
				template.Kills.Text = Kills
				
				template.Parent = script.Parent				
			end			
		end)
	end
end

Thank you so much this means the world to me if you can help me find the issue to this problem. :heart:

1 Like

You may want to use WaitForChild vs FindFirstChild on Humanoid.

2 Likes

nothing really changed. anything else?

Really don’t like how this is being called. Think they are going to remove this at some point anyways.
Hope someone can help out here. I have way too many of these to create and save a new one just to test. Good luck.

2 Likes

Thank you! Hopefully i get an answer soon. Thanks for your help regardless Jay!

1 Like

I will make a blind guess however …

So not tested
local DataStoreService = game:GetService("DataStoreService")
local KillsDataStore = DataStoreService:GetDataStore("KillsData")

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

    local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    kills.Parent = leaderstats

    local success, data = pcall(function()
        return KillsDataStore:GetAsync(player.UserId) or 0
    end)

    if success then
        kills.Value = data
    end

    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            local killer = humanoid:FindFirstChild("creator")
            if killer and killer:IsA("Player") then
                local killerKills = killer.leaderstats:FindFirstChild("Kills")
                if killerKills then
                    killerKills.Value = killerKills.Value + 1
                end
            end
        end)
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local kills = player.leaderstats:FindFirstChild("Kills")
    if kills then
        local success, errorMsg = pcall(function()
            KillsDataStore:SetAsync(player.UserId, kills.Value)
        end)
    end
end)

local function updateLeaderboard()
    local success, errorMsg = pcall(function()
        local sortedPlayers = {}

        for _, player in ipairs(game.Players:GetPlayers()) do
            local kills = player.leaderstats:FindFirstChild("Kills")
            if kills then
                table.insert(sortedPlayers, {Player = player, Kills = kills.Value})
            end
        end

        table.sort(sortedPlayers, function(a, b)
            return a.Kills > b.Kills
        end)

        for i, data in ipairs(sortedPlayers) do
            local template = script.Template:Clone()
            template.PlrName.Text = data.Player.Name
            template.Rank.Text = "#" .. i
            template.Kills.Text = data.Kills
            template.Parent = script.Parent
        end
    end)
    if not success then
        warn("Failed to update leaderboard: " .. errorMsg)
    end
end

updateLeaderboard()
game.Players.PlayerAdded:Connect(updateLeaderboard)
game.Players.PlayerRemoving:Connect(updateLeaderboard)

Little bit different set up …

I take it that didn’t work.

2nd shot at it
local DataStoreService = game:GetService("DataStoreService")
local KillsDataStore = DataStoreService:GetDataStore("KillsData")

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

	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats

	local success, data = pcall(function()
		return KillsDataStore:GetAsync(player.UserId) or 0
	end)

	if success then
		kills.Value = data
	end

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			local killer = humanoid:FindFirstChild("creator")
			if killer and killer:IsA("Player") then
				local killerKills = killer.leaderstats:FindFirstChild("Kills")
				if killerKills then
					killerKills.Value = killerKills.Value + 1
				end
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local kills = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Kills")
	if kills then
		local success, errorMsg = pcall(function()
			KillsDataStore:SetAsync(player.UserId, kills.Value)
		end)
	end
end)

local function updateLeaderboard()
	local success, errorMsg = pcall(function()
		local sortedPlayers = {}

		for _, player in ipairs(game.Players:GetPlayers()) do
			local kills = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Kills")
			if kills then
				table.insert(sortedPlayers, {Player = player, Kills = kills.Value})
			end
		end

		table.sort(sortedPlayers, function(a, b)
			return a.Kills > b.Kills
		end)

		for i, data in ipairs(sortedPlayers) do
			local template = script.Template:Clone()
			template.PlrName.Text = data.Player.Name
			template.Rank.Text = "#" .. i
			template.Kills.Text = data.Kills
			template.Parent = script.Parent
		end
	end)
	if not success then
		warn("Failed to update leaderboard: " .. errorMsg)
	end
end

updateLeaderboard()
game.Players.PlayerAdded:Connect(updateLeaderboard)
game.Players.PlayerRemoving:Connect(updateLeaderboard)

A little bit of testing.

1 Like

For the first shot; nothing changed unfortunately
For the second shot; it completely broke the leaderboard.

I wonder what caused it to do that. Hmm

I’m going to PM you one of mine …

1 Like