Random Vote Script Help

Hey there,

I’ll just get right to it. I need to make a way to randomly choose a value if there is a tie in my voting script.

local votes = {game.ReplicatedStorage.HomeTownDerby, game.ReplicatedStorage.ElectricStretch, game.ReplicatedStorage.SwitchSwapRaceWay, 
		game.ReplicatedStorage.RockyRavine, game.ReplicatedStorage.ObstacleSprint, game.ReplicatedStorage.CrystalCaverns}
	
	table.sort(votes, function(a,b)
		return a.Value > b.Value
	end)
	local chosen = votes[1].Name
	if chosen == "HomeTownDerby" then
		Which.Value = "Home Town Derby"
	elseif chosen == "ElectricStretch" then
		Which.Value = "Electric Stretch"
	elseif chosen == "SwitchSwapRaceWay" then
		Which.Value = "Switch Swap Raceway"
	elseif chosen == "RockyRavine" then
		Which.Value = "Rocky Ravine"
	elseif chosen == "ObstacleSprint" then
		Which.Value = "Obstacle Sprint"
	else
		Which.Value = "Crystal Caverns"
	end

It items in the votes table are referencing int values in replicated storage, and which ever is the highest number is put first in using the table.sort. Is there some way I can check if there is a tie, and then choose a random one? I thought of adding something like this,

if votes[1] == votes[2] then
		
elseif votes[1] == votes[3] then
		
end

and then choosing a random one from that, I have two problems with that though, I don’t know how to sort only those two items, and if I do it that way there might be a three way tie and it won’t go in the random number. So, can you help me figure out how to fix my way of doing it, or even better if you have your own way of doing it, either way do you know how I can figure this out?

You can use several techniques to improve your code.

First one is removing the excessive statements for these

If you notice there is a pattern, chosen is equal to a string, and you are setting Which.Value equal to this string. Consequently you can just do this instead doing an if statement.

Which.Value = chosen  --replaces all those if statements

For the tie breaker here is how it should work.

local highestAmountOfVotes = votes[1].Value--highest amount of votes since it's sorted
--guranteed to be in first index

local mapWithHighestVotes = {}--store maps that tied in a table, to cover the three way tie scenario or even four or five

--in a for loop skip the first vote, we already know who is first
for i = 2, #votes do
	local currentMapBeingChecked = votes[i]
	local mapVote = currentMapBeingChecked .Value
	if mapVote == highestAmountOfVotes  then -- check for tie
		table.insert(mapWithHighestVotes ,currentMapBeingChecked)--insert it into the table
	end
end

local chosen = mapWithHighestVotes[math.random(1,#mapWithHighestVotes)]
2 Likes

Thanks I will go try it out! Just from looking at it I am pretty sure it will work for me. As for the if else statements, you would be correct, except for the fact that I actually have spaces in between the words, and I already have all that code connected with other scripts with the spaces. When I saw your method, I was like duh, I am such a dummy, but then I remembered why I did that :laughing::laughing:.

Ohhh, I just realized as well.

But this issue can also be easily resolved using string manipulation because there is a pattern, there is a space behind each capital letter which is already solved here:

1 Like

Ahh ok I will try that as well! Thanks!