I have this function that chooses the biggest Value from a set of "NumberValues", but how can I make it so if the two biggest numbers are the same, the script chooses a value from the two randomly?
function Greatest(child) local number, map = 0, nil local Vote1 = game:GetService("ReplicatedStorage"):WaitForChild("MapVote1") local Vote2 = game:GetService("ReplicatedStorage"):WaitForChild("MapVote2") local Vote3 = game:GetService("ReplicatedStorage"):WaitForChild("MapVote3") for _, v in pairs(child) do if v:IsA("NumberValue") and v.Value > number then number = v.Value map = v end end return number, map end
the simplest way I found was to use math.max
. basically, it takes a bunch of numbers and finds which is the greatest (so if I did math.max(1,2,3,5,67,7,4,3,6,7,4) it would return "67")
function Greatest(child) local number, map = 0, nil local Vote1 = game:GetService("ReplicatedStorage"):WaitForChild("MapVote1") local Vote2 = game:GetService("ReplicatedStorage"):WaitForChild("MapVote2") local Vote3 = game:GetService("ReplicatedStorage"):WaitForChild("MapVote3") number = math.max(Vote1,Vote2,Vote3) for i,v in pairs(child) do if v:IsA("NumberValue") then if v.Value == number then map = v end end end return number, map end