Voting system problem

ive tried to make a voting system but sometimes it can tie on the lowest votes and result in 1 of them which would be a problem
the script :

local HideSeekVote = script.HideSeek
local BombVote = script.Bomb
local ChaseVote = script.Chase


workspace.GameData.Voting.Changed:Connect(function()
	if workspace.GameData.Voting.Value == 0 then
		local Votes = {HideSeekVote,BombVote,ChaseVote}
		local Highest = 0
		local HighestType
		local Ties = {}
		for i,v in pairs(Votes) do
			if v.Value.Votes.Value > Highest then
				 Highest = v.Value.Votes.Value
				HighestType = v.Name
			elseif Highest == v.Value.Votes.Value then
				table.insert(Ties,v.Name)
				table.insert(Ties,HighestType)
			end
		end
		local result
		if #Ties > 0 then
			result = "B" .. Ties[math.random(1,#Ties)]
		else
			result = "B" .. HighestType
		end
		script:FindFirstChild(result).Value.Enabled = true
		game.ReplicatedStorage.VoteWin:FireAllClients(script:FindFirstChild(result).Value.Parent)
		task.wait(2)
		script:FindFirstChild(result).Value.Enabled = false
	end
end)
1 Like

Why did u put an hashtag next to if ties

To get all of the item as a number by how many exist

1 Like

can you explain further? Character Limit

So lets say 1 person votes for 1 mode but since 2 others tie it could also choose them

bumping since its been 8 hours

this is a video of what i mean

How does the plate change from the original material to neon? Can you give the script for that functionality, if you haven’t already?

that happens when you hit it on the client and the result comes from the remote event firing all the client to the server but sure here is the script of the plates turning neon

local Buttons = {script.Button1.Value,script.Button2.Value,script.Button3.Value}

game.ReplicatedStorage.VoteWin.OnClientEvent:Connect(function(Button)
	
	game:GetService("TweenService"):Create(Button,TweenInfo.new(0.25,Enum.EasingStyle.Linear),{Color = Color3.fromRGB(133, 200, 133)}):Play()
	for i,v in pairs(Buttons) do
		if v.Clicky ~= Button then
			game:GetService("TweenService"):Create(v.Clicky,TweenInfo.new(0.25,Enum.EasingStyle.Linear),{Color = Color3.fromRGB(125, 125, 125)}):Play()
		end
	end
	task.wait(4)
	game:GetService("TweenService"):Create(Button,TweenInfo.new(0.25,Enum.EasingStyle.Linear),{Color = Color3.fromRGB(125, 125, 125)}):Play()
end)


function GetPlayerByHit(Hit:Instance,Debug:boolean)
	if Hit:FindFirstAncestorOfClass("Model") and game.Players:GetPlayerFromCharacter(Hit:FindFirstAncestorOfClass("Model")) ~= nil then
		if Debug == true then
			warn("Hit is from a character/player | ".. Hit:GetFullName())
		end
		local Character = Hit:FindFirstAncestorOfClass("Model")
		local Humanoid
		local Player = game.Players:GetPlayerFromCharacter(Character)
		local HumanoidRoot 
		if Character:FindFirstChildOfClass("Humanoid") ~= nil then
			Humanoid = Character:FindFirstChildOfClass("Humanoid")
		end
		if Character:FindFirstChild("HumanoidRootPart") ~= nil then
			HumanoidRoot = Character:FindFirstChild("HumanoidRootPart")
		end
		return {Character = Character, Humanoid = Humanoid,Player = Player,HumanoidRoot = HumanoidRoot}
	else
		if Debug == true then
			warn("Hit is not a character/player")
		end
		return {Character = nil, Humanoid = nil,Player = nil,HumanoidRoot = nil}
	end
end



for i,v in pairs(Buttons) do
	v.Clicky.Touched:Connect(function(Hit)
		if workspace.GameData.Voting.Value > 0 then
		local Info = GetPlayerByHit(Hit,true)
		if Info.Player == game.Players.LocalPlayer then
			game:GetService("TweenService"):Create(v.Clicky,TweenInfo.new(0.25,Enum.EasingStyle.Linear),{Color = Color3.fromRGB(100, 150, 100)}):Play()
			for i2,v2 in pairs(Buttons) do
				if v2.Clicky ~= v.Clicky then
					game:GetService("TweenService"):Create(v2.Clicky,TweenInfo.new(0.25,Enum.EasingStyle.Linear),{Color = Color3.fromRGB(125, 125, 125)}):Play()
				end
			end
			end
			end
	end)
end 

just saying the main problem is at the script i gave all the votes are gathered correctly but i cant seem to get the result correctly since it can tie with the highest and the lowest which could make it vote something random

1 Like

After heavily looking at the script, I think I might know why the lowest matching votes may be randomly being picked. This could be because even when the highest vote has been found, the lowest votes are still being included in the table when using math.random(), meaning that it could still be randomly choosing the lowest numbers, as well.

You could try fixing this, by clearing the tiles table of the lowest votes every time the highest vote has been found. Tell me if it fixes the problem:

local HideSeekVote = script.HideSeek
local BombVote = script.Bomb
local ChaseVote = script.Chase

workspace.GameData.Voting.Changed:Connect(function()
	if workspace.GameData.Voting.Value == 0 then
		local Votes = {HideSeekVote,BombVote,ChaseVote}
		local Highest = 0
		local HighestType
		local Ties = {}
		for i,v in pairs(Votes) do
			if v.Value.Votes.Value > Highest then
				Highest = v.Value.Votes.Value
				HighestType = v.Name
				Ties = {}
			elseif Highest == v.Value.Votes.Value then
				table.insert(Ties,v.Name)
				table.insert(Ties,HighestType)
			end
		end
		local result
		if #Ties > 0 then
			result = "B" .. Ties[math.random(1,#Ties)]
		else
			result = "B" .. HighestType
		end
		script:FindFirstChild(result).Value.Enabled = true
		game.ReplicatedStorage.VoteWin:FireAllClients(script:FindFirstChild(result).Value.Parent)
		task.wait(2)
		script:FindFirstChild(result).Value.Enabled = false
	end
end)

Correct me if I am wrong though, but it’s just a theory. I hope that it solves your issue.

1 Like

Thanks this worked even though i couldn’t see the diffrence in my script and this one

I added the Tiles = {} line to remove any lower votes, before randomly choosing on the highest vote, so the lowest votes can’t be chosen:

for i,v in pairs(Votes) do
			if v.Value.Votes.Value > Highest then
				Highest = v.Value.Votes.Value
				HighestType = v.Name
				Ties = {}
			elseif Highest == v.Value.Votes.Value then
				table.insert(Ties,v.Name)
				table.insert(Ties,HighestType)
			end
		end

oh okay thanks for letting me know the change

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