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

Choosing the Biggest Value?

Asked by 5 years ago

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
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 Mr_Pure 129 — 5y
0
table.sort and array :p theking48989987 2147 — 5y
0
@Mr_Pure, can you show me an example? You can do it with just plain numbers or anything you feel comfortable with. top500widowxd 23 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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

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. Mr_Pure 129 — 5y
Ad

Answer this question