Highest leaderstat player gets Crown

So I have a crown that is supposed to be given to the player who has the highest “Time” stat in the game. How would I do that? This is my current script. It does it for the first time when a player dies. After the player is dead, it doesn’t work again. Any feedback will be appreciated.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)		
		while wait(1)do
		local function getMax(stat)
			local player = nil
			local value = -math.huge
			local temp
				for _, p in ipairs(game.Players:GetPlayers()) do
					temp = p.leaderstats.Time.Value
					if (temp > value) then
					player = p
					value = temp
					end
				end
			return player
			end
			
			local richestPlayer = getMax("Time")
				if (richestPlayer) 
			then 
				local clone = script.Parent.Parent:Clone() 
				clone.Handle.Anchored = false
				clone.Parent = character
				
			end
		end
	end)
end)
1 Like

Maybe try this method instead:

Insert the crown into ReplicatedStorage. Then, in a ServerScript inside of ServerScriptService, write:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local crown = ReplicatedStorage:WaitForChild("Crown")

while wait(1) do
    local player
    local high = 0

    for _, plr in pairs(game.Players:GetPlayers()) do
        local value = plr.leaderstats.Time.Value
        if value > high then
            player = plr
            high = value
        end
    end

    if player then
        local character = player.Character
        if character then
            for _, plr in pairs(game.Players:GetPlayers()) do
                local char = plr.Character
                if char then
                    if char:FindFirstChild("Crown") then
                        char.Crown:Destroy()
                    end
                end
            end

            local newCrown = crown:Clone()
            newCrown.Parent = player.Character
        end
    end
end

Let me know if you have any issues with this method, I have not tested it.

3 Likes

Thanks a lot! It worked! Marked it as the solution. :slight_smile:

1 Like