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

Choosing the Biggest Value?

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

3 comments

0

It may be a bit tedious but you can put them in a table, set a variable to math.random(1,#tablelength) and have it index the map chosen

0

table.sort and array :p

0

@Mr_Pure, can you show me an example? You can do it with just plain numbers or anything you feel comfortable with.

1 answer

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

1 comment

0

@the8bitdude11 she's/he's asking how she/he would solve this problem, not get to it. If she/he had two maps with the same number how would she/he fix that, that's his/her question.

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