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

What do i use to determine which four different int values are higher?

Asked by
Neu_Dev 22
4 years ago

What do i use to determine which four different int values are higher? and choose that value?

i tried doing for loops i just can't wraped my head around.

3 answers

Log in to vote
3
Answered by 4 years ago

I'm no rocket scientist, but I'm pretty sure you can just use math.max for this sort of thing.

local highest = math.max(1,2,3,4,5,6,7,8,9) --//You can put whatever numbers you want in there.

print(highest) --//Will output 9

You can also put the numbers inside of a table, and using the unpack function can unload them into the function like this.

local numbers = {1,2,3,4,5,6,7,8,9}
local highest = math.max(unpack(numbers)) --//Is the same as the code from before, but this time we're taking all the values from the table and unpacking them into the math.max function.

print(highest) --//Still outputs 9

Not sure if I'm reading the question wrong or not judging from the above answers, but the math.max function can certainly get the largest number from a group of numbers. Conversely there is the math.min function, which does the same thing but returns the smallest number.

All the math functions

0
You can just replace the numbers 1-9 with how many numbers and whatever numbers you want. CeramicTile 847 — 4y
2
Your answer is underrated programmerHere 371 — 4y
Ad
Log in to vote
-1
Answered by
Alphexus 498 Moderation Voter
4 years ago

This is quite simple.

local function SortIntValues(folder)
     local intValues = {}
     for i,v in pairs(folder:GetChildren()) do
          table.insert(intValues, v)
     end
     table.sort(intValues, function(a,b) return a.Value > b.Value end)
     return intValues[1]
end

local HighestIntValue = SortIntValues(folderWithIntValues) — This is the highest int value.

0
i quite get it but not fully enough, but thank you! ill use this for knowledge in the long run. Neu_Dev 22 — 4y
1
With all due respect my godly lad, giving fully working code and not even explaining it is going to help him now but he can't learn from this. User#25069 0 — 4y
0
The reason I recommend this way is from math.max and Virality’s way is that it returns the actual IntValue. The other two answers are only returning the highest number but I don’t see what you can do with just a number. Alphexus 498 — 4y
1
You can do lots of stuff, like math programmerHere 371 — 4y
Log in to vote
-2
Answered by 4 years ago

I'm kind of tired right now so if there is a predefined method for this somebody tell me, but here is a function you can use:

local one = 1
local two = 2
local three = 3
local four = 4

local values = {
    one,
    two,
    three,
    four,
}

function getHighest(table)
    highest = 0
    for i,v in pairs(table) do
        if v > highest then
            highest = v
        end
    end
    return highest
end
0
My god you're right.. why i have never think about this...i did a different method still works but i rather use this so its more computing power friendly thank you! Neu_Dev 22 — 4y
0
Np xxViralityxx -9 — 4y
0
-1 for reinventing math.max programmerHere 371 — 4y
0
programmerHere, this forum isn't for you man, this isn't a forum to shame others on their mistakes, it's about learning and creating a positive community. Robowon1 323 — 4y
View all comments (2 more)
0
Bad answer. -1. DeceptiveCaster 3761 — 4y
0
I'm here to correct mistake bro programmerHere 371 — 4y

Answer this question