Get the player with the most int value

Hello, im trying to get the player with the most value in the server .

Here is what im doing so far

	local Winner = 
	print(Winner)
	local dancingWinner = player.Character
	dancingWinner.Parent = game.Workspace
	dancingWinner.Name = "Winner"
	dancingWinner.HumanoidRootPart.Position = workspace.tpPart.Position
	local dance = dancingWinner:WaitForChild("Humanoid"):LoadAnimation(script.Animation)
	dance.Looped = true
	dance:Play()
1 Like

Hello,
You can do this fairly easy by going over all the players and returning the player with the highest value.

local Winner,highest = nil,0
for _,plr in pairs(game.Players:GetPlayers()) do
	local v = plr.leaderstats.Coins.Value	--change this to the path from your player to their value
	if v>highest then 
		Winner=plr
		highest=v
	end
end)

Make sure you change the local v = to the correct value you want to check from the player.

it gives me an error with Winner,highest = nil,0

Please show me the error that you’re seeing in the output

i think its a missing )


but i dont know where is it

1 Like

Oops, my bad, please remove the ) at the end of the code snippet I sent you.

1 Like

Okay im gonna try that out thanks !

It works but, when i try to get the character it says “ServerScriptService.GameHandler:107: attempt to index nil with ‘Character’”

Nevermind nvm thats just a me problem

its pretty unrelated but how do i put every player in a table?

Well you can use table.insert for that, but you can also just get a table with all players by doing

game.Players:GetPlayers()

i did, but it prints out “table: 0x1488bf4b468ce07a”

by the way when i try to name the player character winner it says “The current thread cannot set ‘Character’s name’ (lacking capability WritePlayer)” i never saw that before

Your script can read the table, but if you want to print the table out you’ll have to do it for each object one by one.

for i,v in pairs(game.Players:GetPlayers()) do
	print(v)
end

Instead of doing Character.Name=, try Character.Humanoid.DisplayName=.

i specially wanted to change the model name to avoid any bugs

With all due respect, you will encounter more bugs if you choose to rename the character itself, I highly recommend just changing the displayname. If you need to check wether the character is the winner, don’t use the name for that, just either give it a BoolValue set to true with the name “Winner”, or add a new boolvalue attribute to the character with the name “Winner”, also set to true. (option 1 is much easier)