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

How Do I Find the Greatest Value in an Array?

Hello, I am making a deathmatch mode in my game, and of course, kills will be a number. If I put them in an array, is there a command in whch I can find the greatest value? Thanks, ~Yoman1776

1 answer

✓ Accepted answer

Hmm, normally I wouldn't a question like this, since you haven't shown any effort. However, the script is simple:

I recommend writing a generic for loop that iterates through the table (goes through it), and compares all of the values to each other, returning the largest one. I'm not sure if there's a simple way, but this is my way: (I didn't test this since I'm not home, but it should work)

local My_Table = {1,7,4,30,5}

function FindGreatestValue()
	local greatest = 0
	for _,v in pairs(My_Table) do
		if (tonumber(v) > tonumber(greatest)) then -- Tonumber isn't needed, but in some cases it is
			greatest = tonumber(v)
		end
	end
	return greatest
end

print(FindGreatestValue()) -- Will print what it returns, which would be 30

1 comment

0

Hey, sorry for what seems lack of effort, but I searched through forums and the wiki and no results. This will definitely do, thanks for answering!

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