How would I easily detect these values?

Hello, I am making a map voting system for practice, and I do not know how to detect if Map1Votes is greater than Map2Votes or if Map2Votes is greater than Map3Votes.

If you don’t know what I am talking about, here is the code I wrote;

local work = game:GetService("Workspace")
local MapsFolder = work.MapVoting

while task.wait(0.1) do
	if MapsFolder.Voting.Value == true then

		task.wait(10) -- Wait how long the voting lasts
		
		local Map1 = MapsFolder.Map1
		local Map2 = MapsFolder.Map2
		local Map3 = MapsFolder.Map3
		
		local Map1Value = Map1.Votes.Value
		local Map2Value = Map1.Votes.Value
		local Map3Value = Map1.Votes.Value
		
-- here I want to detect which is the highest value.

	end
end
1 Like

Perhaps you could add the values inside a table then use table.sort and sort it like: a > b

1 Like

Not sure how tables work, I will check though.

1 Like

This is the way I would do it.

Using if statements and checking if the votes are higher than the most votes.

local work = game:GetService("Workspace")
local MapsFolder = work.MapVoting

local MostVotes = 0
local CurrentMap = "Map1"

while task.wait(0.1) do
	if MapsFolder.Voting.Value == true then

		task.wait(10) -- Wait how long the voting lasts
		
		MostVotes = 0
		
		CurrentMap = ""
		
		local Map1 = MapsFolder.Map1
		local Map2 = MapsFolder.Map2
		local Map3 = MapsFolder.Map3

		local Map1Value = Map1.Votes.Value
		local Map2Value = Map1.Votes.Value
		local Map3Value = Map1.Votes.Value

		if Map1Value > MostVotes then
			CurrentMap = "Map1"
			MostVotes = Map1Value
		else
			if Map2Value > MostVotes then
				CurrentMap = "Map2"
				MostVotes = Map2Value
			else
				if Map3Value > MostVotes then
					CurrentMap = "Map3"
					MostVotes = Map3Value
				end
			end
		end

	end
end

In the code above I used Map1, Map2, and Map3 as strings, but you can set the maps as the actual CurrentMap Value.

1 Like

Try this:

local work = game:GetService("Workspace")
local MapsFolder = work.MapVoting

while task.wait(0.1) do
	if MapsFolder.Voting.Value == true then

		task.wait(10) -- Wait how long the voting lasts

local mapvotes = {}

for _,v in pairs(MapsFolder:GetChildren()) do
    mapvotes[#mapvotes + 1] = {Map = v.Name; Value = v.Votes.Value}
end

table.sort(mapvotes, function(a, b)
    return a.Value > b.Value
end)

print(mapvotes[1]) -- prints the highest-voted map
end
1 Like

You should be printing mapvotes[1].Map not mapvotes[1][1]

2 Likes

Ah yes srry, thanks for correcting my mistake! I’ll change it right away.

it says there is an error in that area.

1 Like
local work = game:GetService("Workspace")
local MapsFolder = work.MapVoting

while task.wait(0.1) do
	if MapsFolder.Voting.Value == true then

		task.wait(10) -- Wait how long the voting lasts
		
		local Map1 = MapsFolder.Map1
		local Map2 = MapsFolder.Map2
		local Map3 = MapsFolder.Map3
		
		local Map1Value = Map1.Votes.Value
		local Map2Value = Map1.Votes.Value
		local Map3Value = Map1.Votes.Value
                print(math.max(Map1Value, Map2Value, Map3Value))
                
	end
end```
1 Like

Could u tell me what it printed in output?

1 Like