How can I get the highest valued NumberValue in a folder?

Hi. I’m trying to make a difficulty selector where players vote, and I have no idea where and how to start to get the highest voted difficulty.

image

These are all numbervalues under a single folder.

I tried this, which I’m pretty sure would half work, But I realized it wouldn’t get the name of the difficulty, which I need.

local DiffVotes = {}
	for i, v in pairs(game.ReplicatedStorage.Values.Votes:GetChildren()) do
		if v:IsA("NumberValue") then
			table.insert(DiffVotes, v.Value)
		end
	end
	wait()
	local highestnumber = math.max(table.unpack(DiffVotes))

Any help is appreciated.

2 Likes

Here is what I came up with:

local highestObject
local highestNumber = 0
for i, v in pairs(game.ReplicatedStorage:FindFirstChild('Values').Votes:GetChildren()) do
	
	if (v:IsA('NumberValue')) then
		
		if (v.Value > highestNumber) then
			
			highestNumber = v.Value
			
			highestObject = v
			
		end
		
	end
	
end
1 Like

Here:

function GetHighestVote()
	local number = 0 -- to find the highest number
	local currentInst -- to get the instance with that number
	
	for i,v in pairs(game.ReplicatedStorage.Values.Votes:GetChildren()) do
		if v.Value > number then
			number = v.Value
			currentInst = v
		end
	end
	return currentInst
end

If you don’t already know, you can get the name and value by doing this

name = GetHighestVote().Name
value = GetHighestVote().Value
1 Like