Voting system bug [SOLVED BY ME XD]

So my voting system almost works, but there’s a slight problem. Ill just show the video.

-- Client side
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local RE = game.ReplicatedStorage:WaitForChild("CalculateVotes")
local Game1 = PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Game1")
local Game2 = PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Game2")

Game1.MouseButton1Click:Connect(function()
	local Game1Votes = Player:GetAttribute("Game1Votes")
	
	if Game1Votes and Game1Votes == 0 then
		RE:FireServer("Game1Votes", 1, Game1)
	end
	
	local Game2Votes = Player:GetAttribute("Game2Votes")
	
	if Game2Votes and Game2Votes == 1 then
		RE:FireServer("Game2Votes", 0, Game2)
		
		RE:FireServer("Game1Votes", 1, Game1)
	end
end)

Game2.MouseButton1Click:Connect(function()
	local Game2Votes = Player:GetAttribute("Game2Votes")

	if Game2Votes and Game2Votes == 0 then
		RE:FireServer("Game2Votes", 1, Game2)
	end
	
	local Game1Votes = Player:GetAttribute("Game1Votes")

	if Game1Votes and Game1Votes == 1 then
		RE:FireServer("Game1Votes", 0, Game1)
		
		RE:FireServer("Game2Votes", 1, Game2)
	end
end)
-- Server side
local RepClients = game.ReplicatedStorage.ReplicateClients
local Calculations = game.ReplicatedStorage.CalculateVotes
local TotalGame1Votes = 0
local TotalGame2Votes = 0

Calculations.OnServerEvent:Connect(function(Player, ReceivedAttribute, Votes, ChosenGame)
	if Player then
		Player:SetAttribute(ReceivedAttribute, Votes)
		print(ReceivedAttribute..":"..Player:GetAttribute(ReceivedAttribute))
		if ReceivedAttribute == "Game1Votes" then
			local Game1Votes = Player:GetAttribute(ReceivedAttribute)
			if Game1Votes == 0 then
				TotalGame1Votes -= 1
				print(TotalGame1Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame1Votes)
			else
				TotalGame1Votes += Game1Votes
				print(TotalGame1Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame1Votes)
			end
		end
		if ReceivedAttribute == "Game2Votes" then
			local Game2Votes = Player:GetAttribute(ReceivedAttribute)
			if Game2Votes == 0 then
				TotalGame2Votes -= 1
				print(TotalGame2Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame2Votes)
			end
			if Game2Votes == 1 then
				TotalGame2Votes += Game2Votes
				print(TotalGame2Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame2Votes)
			end
		end
	end
end)

I used print statements to debug the problem and found out the issue is totalvotes, so I realised it was because I wasn’t checking the previous votes I guess. So I changed the script to this:

local RepClients = game.ReplicatedStorage.ReplicateClients
local Calculations = game.ReplicatedStorage.CalculateVotes
local TotalGame1Votes = 0
local TotalGame2Votes = 0

local StorePreviousVotes = {}

Calculations.OnServerEvent:Connect(function(Player, ReceivedAttribute, Votes, ChosenGame)
	if Player then
		Player:SetAttribute(ReceivedAttribute, Votes)
		print(ReceivedAttribute..":"..Player:GetAttribute(ReceivedAttribute))
		if ReceivedAttribute == "Game1Votes" then
			local Game1Votes = Player:GetAttribute(ReceivedAttribute)
			if Game1Votes == 0 and StorePreviousVotes[Player.UserId] then
				TotalGame1Votes -= 1
				print(TotalGame1Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame1Votes)
				table.remove(StorePreviousVotes, Player.UserId)
			end
			if Game1Votes == 1 and not StorePreviousVotes[Player.UserId] then
				TotalGame1Votes += Game1Votes
				print(TotalGame1Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame1Votes)
				table.insert(StorePreviousVotes, Player.UserId)
			end
		end
		if ReceivedAttribute == "Game2Votes" then
			local Game2Votes = Player:GetAttribute(ReceivedAttribute)
			if Game2Votes == 0 and StorePreviousVotes[Player.UserId] then
				TotalGame2Votes -= 1
				print(TotalGame2Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame2Votes)
				table.remove(StorePreviousVotes, Player.UserId)
			end
			if Game2Votes == 1 and not StorePreviousVotes[Player.UserId] then
				TotalGame2Votes += Game2Votes
				print(TotalGame2Votes)
				RepClients:FireAllClients(ChosenGame.Name, TotalGame2Votes)
				table.insert(StorePreviousVotes, Player.UserId)
			end
		end
	end
end)

But then it just made it even worse, before it was atleast subtracting the last vote. Now it just does the same thing but doesn’t subtract it. I hope someone knows the fix for this without changing my script too much?

1 Like

Also I will later change this to just server-sided script because I realised if i’m using attributes I don’t need to do this on the client. But the client isn’t the problem its the way I’m adding the totalvotes so please someone help?

Someone help please? Anyone? Anyone at all?

I guess nobody knows the answer?

You could try having a couple different tables which represent different voting options (not too much different from what you already have) and send a remote event with the prompt they clicked. Then check if the player is in either table and go from there? The amount of votes for a option would be the amount of players in the table. I think you’re overcomplicating this.

1 Like

Sorry if I am overcomplicating, it’s because I don’t really watch tutorials on like “how to make a voting system” I try myself instead. Could you explain further what you mean by this? Maybe with some example code?

Alright quick update I found the solution, It was this thanks to one of my friends, who wasn’t even on the devforum. Surprising because I thought you guys would’ve found it first.

As you can see I was accidently adding it twice. Other than that my code is fine.

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