Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How Do I Find the Greatest Value in an Array?

Asked by 8 years ago

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

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

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
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! yoman1776 85 — 8y
Ad

Answer this question