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?