How can I get the map with the highest votes correctly?

Hey!

So I made a script which decides what’s the map with the highest votes.
However, it’s not optimized, and it has some issues.
So I wanna know I can improve the way I did it.


My current code

local chosenMap

if blocksVotes.Value > cityVotes.Value and blocksVotes.Value > forestVotes.Value and blocksVotes.Value > skylandsVotes.Value then
	chosenMap.Value = blocksVotes.Parent.Name
end
if cityVotes.Value > blocksVotes.Value and cityVotes.Value > forestVotes.Value and cityVotes.Value > skylandsVotes.Value then
	chosenMap.Value = cityVotes.Parent.Name
end
if forestVotes.Value > cityVotes.Value and forestVotes.Value > blocksVotes.Value and forestVotes.Value > skylandsVotes.Value then
	chosenMap.Value = forestVotes.Parent.Name
end
if skylandsVotes.Value > forestVotes.Value and skylandsVotes.Value > cityVotes.Value and skylandsVotes.Value > blocksVotes.Value then
	chosenMap.Value = skylandsVotes.Parent.Name
end

Thanks, bye!

2 Likes

you can use this

function PickHigh(fv1)
	local High = 1
    for i, v in pairs(fv1) do
       if v > fv1[High] then
           High = i
       end
    end
	return fv1[High]
end
chosenMap.Value = PickHigh({blocksVotes.Value, cityVotes.Value, forestVotes.Value, skylandsVotes.Value}).Parent.Name

1 Like

Won’t there be any issues when the votes will be equal?

nope, it will work completely fine. it will choose the first map among the equal maps in that case.

Ex, if cityVotes and forestVotes are equal and they are highest, it will choose cityVotes

1 Like

Oh that’s perfect then!
Lemme test this out and see if it works :smiley:
Thanks a lot for helping!

1 Like

Okay so I tested it.
And the only problem is that it changes the ChosenMap value to a number.
But I want it to be changed to the name of the map

Oh, you can use this code for that

function PickHigh(fv1)
	local High = 1
    for i, v in pairs(fv1) do
       if v.Value > fv1[High] then
           High = i
       end
    end
	return fv1[High]
end
chosenMap.Value = PickHigh({blocksVotes, cityVotes, forestVotes, skylandsVotes}).Parent.Name
1 Like

I got this error idk why
Line 208 is this: if v.Value > fv1[High] then

can you send screen short of explorer where blocksVotes and stuffs are present

1 Like

image
VotesAmount is the amount of votes for that map

And blocksVotes is a variable for Blocks.VotesAmount ?

1 Like

Yes exactly

Oh sorry, got the error. here’s the fixed code

function PickHigh(fv1)
	local High = 1
    for i, v in pairs(fv1) do
       if v.Value > fv1[High].Value then
           High = i
       end
    end
	return fv1[High]
end
chosenMap.Value = PickHigh({blocksVotes, cityVotes, forestVotes, skylandsVotes}).Parent.Name
2 Likes

Yep, working perfectly fine now!
Thank you so much for staying and helping me till the end :smiley:
you legend

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.