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

I'm having a bit of problems. Can someone point me in the right direction?

So I'm trying to make a piece of code, that basically says 3 numbers in a table, and I want the script to print the highest number. I am really confused so if someone could help me that'd be great!

local Table {1,2,3}
for i,v in pairs(Table) do
print(Table.MaxValue)
end

1 comment

0

missing equals on line 1

1 answer

local Table = {1,2,3}

local HighestValue = 0
for i,v in pairs(Table) do -- iterate through table
	if v > HighestValue then -- check if value in table is greater than the highest value so far
		HighestValue = v -- sets highestvalue if it his greater than
	end
end
print(HighestValue) -- prints 3 (the highest number) to output

You could also make it into a function

function GetHighestNumber(Table)
	local HighestValue = 0
	for i,v in pairs(Table) do
		if v > HighestValue then
			HighestValue = v
		end
	end
	return HighestValue
end

local Table1 = {1,2,3}
local HighestNumber = GetHighestNumber(Table1)
print(HighestNumber) -- Prints 3

1 comment

0

Good work.

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