Voting system help

I’m trying to make a voting thing for mayor but i can’t figure out how to make it count the votes, i want to make it so that you just have to stand on a vote pad once and it will count (opposed to stay standing on it) and obviously where if you vote for the other it will remove the previous vote.

Here is the script (commented section is the broken part i think):

local VoteBlue = game.Workspace:FindFirstChild("VoteBlue")
local VoteRed = game.Workspace:FindFirstChild("VoteRed")
local Mayor = game.Workspace:FindFirstChild("Mayor")

local myr = workspace.Mayor
local blueVotes = 0
local redVotes = 0
local candidateBlue = nil
local candidateRed = nil

local mayorColor = nil

local mayorTag = game.ServerStorage:FindFirstChild("MayorTag")
local candidateTag = game.ServerStorage:FindFirstChild("CandidateTag")

VoteBlue.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		local char = hit.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		if candidateBlue == nil and plr ~= candidateRed and plr ~= myr.Value then
			candidateBlue = plr
			local tag = candidateTag:Clone()
			tag.TextLabel.TextColor3 = Color3.fromRGB(13, 105, 172)
			tag.Parent = char["Head"]
			print(plr.Name .. " has become a candidate for the Blue Party.")
		else
			print("Someone is already running for the Blue Party.")
		end
	end
end)

VoteRed.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		local char = hit.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		if candidateRed == nil and plr ~= candidateBlue and plr ~= myr.Value then
			candidateRed = plr
			local tag = candidateTag:Clone()
			tag.TextLabel.TextColor3 = Color3.fromRGB(196, 40, 28)
			tag.Parent = char["Head"]
			print(plr.Name .. " has become a candidate for the Red Party.")
		else
			print("Someone is already running for the Red Party.")
		end
	end
end)



game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if plr == myr.Value then
			local tag = Mayor:Clone()
			tag.TextLabel.TextColor3 = mayorColor
			tag.Parent = char["Head"]
		elseif plr == candidateBlue then
			local tag = candidateTag:Clone()
			tag.TextLabel.TextColor3 = Color3.fromRGB(13, 105, 172)
			tag.Parent = char["Head"]
		elseif plr == candidateRed then
			local tag = candidateTag:Clone()
			tag.TextLabel.TextColor3 = Color3.fromRGB(196, 40, 28)
			tag.Parent = char["Head"]
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if plr == myr.Value then
		myr.Value = nil
	elseif plr == candidateBlue then
		candidateBlue = nil
	elseif plr == candidateRed then
		candidateRed = nil
	end
end)

while true do
	wait(30) -- Wait 5 minutes

	if not(candidateBlue ~= nil and candidateRed ~= nil) then
		--[[for _, v in pairs(VoteBlue:GetTouchingParts()) do
			if v.Parent:FindFirstChild("Humanoid") then
				blueVotes += 1
			end
		end
		
		for _, v in pairs(VoteRed:GetTouchingParts()) do
			if v.Parent:FindFirstChild("Humanoid") then
				redVotes += 1
			end
		end]]

		print("Blue Party: " .. #blueVotes .. " votes")
		print("Red Party: " .. #redVotes .. " votes")

		if #blueVotes > #redVotes then
			Mayor.Value = candidateBlue
			print("Blue Party wins!")
			
			mayorColor = Color3.fromRGB(13, 105, 172)
			local tag = mayorTag:Clone()
			tag.TextLabel.TextColor3 = mayorColor
			tag.Parent = workspace[candidateBlue.Name]["Head"]
		elseif #redVotes > #blueVotes then
			Mayor.Value = candidateRed
			print("Red Party wins!")
			
			mayorColor = Color3.fromRGB(196, 40, 28)
			local tag = mayorTag:Clone()
			tag.TextLabel.TextColor3 = mayorColor
			tag.Parent = workspace[candidateBlue.Name]["Head"]
		else
			print("It's a tie!")
		end
	else
		print("Not enough candidates to start the vote.")
	end

	-- Reset the candidates and votes
	candidateBlue = nil
	candidateRed = nil
	blueVotes = 0
	redVotes = 0
	
	for _, v in pairs(workspace:GetDescendants()) do
		if v.Name == "CandidateTag" then
			v:Destroy()
		end
	end
end

1 Like

Simply create a table that stores each players votes, you could use 0 for neither, 1 for red, and 2 for blue, stepping on either button should locate that player in the table and modify their vote to the one they have chose. Then a for loop to add all the votes up and compare them to find your winner.