Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to find out which player has the most of a certain leaderstat?

I am making a game where if you click people, they will die after a bit. Main script is made but, I wanna know, how do I know which player has the most time in-game? I have made a leaderstats for it.

local Players = game:GetService("Players")

local function leaderboard(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local timeD = Instance.new("IntValue", leaderstats)
	timeD.Name = "Time"
	timeD.Value = 0
	while true do
		wait(1)
		timeD.Value = timeD.Value + 1
	end
end

Players.PlayerAdded:Connect(leaderboard)

Please help.

1 comment

1 answer

local topPlayer = nil --establish topplayer variable

local function checkTop()
	local topValue = 0 --set the initial top value to 0
	for key,plr in pairs (game.Players:GetPlayers())  --getplayers() returns a table. For i,v loop goes through this table
		local leaderstats = plr:FindFIrstChild("leaderstats") 
		if leaderstats then --check if the player has a leaderstats folder
			local timeD = leaderstats:FindFirstChild("timeD")
			if timeD.Value > topValue then --see if the timeD value is greater than the top value
				topValue = timeD.Value --if true then set the top value to the timed value
				topPlayer = plr --and the top player to player
			end
		end
	end
end

checkTop() --write this whenever you want to check who's the top player

Pretty self-explanatory. Let me know if you have any issues.

3 comments

0

Explain next time.

0

cri

0

So, I tried it, didn't work. I am trying to see which player has the most time in-game using a leaderstat, then I wanna copy a BillboardGUI into their head. I know how to do the billboard Gui part, but I still don't know how to check who is the top player (aka the player with most of a leaderstat.), this script, unfortunatly, didn't work.

This discussion is preserved as it was. New answers, comments, and votes are closed.