How would I get the player with the highest value from a list of values?

I’ve been making a minigame-round based game recently.
How the game calculates the players is:
There’s a folder in Lighting that when the round starts, it gets all the players and creates an IntValue for each player, being the IntValue’s name, the playername.
When a player dies, its value gets deleted from the folder in Lighting. Finally the script checks how many values are there in Lighting and just checks for each’s players value to give them 15 points in the leaderstats.
I basically did this because at the time of making the game, I wasn’t really into tables so I just made my own system for it.

Now, I wanted to make a “get the most coins” minigame and, for it, I made it so when a player picks up a coin, its IntValue’s value in the Lighting folder increases. This is where the issue begins.

At the end of the round:
How would I detect which IntValue has the highest value of the folder, and make it return the player associated with it and its IntValue’s value?

I know all of this might be really complicated but its been bugging me out for a few days, thanks.

local LightingFolder = game:GetService(“Lighting”):WaitForChild(“PlayersFolder”)

– Get all the IntValues in the folder
local intValues = LightingFolder:GetChildren()

– Initialize variables to keep track of the highest value and associated player
local highestValue = 0
local winningPlayer = nil

– Loop through each IntValue and compare their values
for i, intValue in ipairs(intValues) do
local player = game.Players:FindFirstChild(intValue.Name)
if player and intValue.Value > highestValue then
highestValue = intValue.Value
winningPlayer = player

– Print out the winning player and their score
if winningPlayer then
print(winningPlayer.Name … " wins with a score of " … highestValue)
print(“No player found with a score.”)

thanks! althought the commas made it weird for the script to read, thank you so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.