- I want a voting system where if Player X votes for Option 1, their name will be added to Table 1. If that same player votes for Option 2, their name will be added to Table 2. Their name is removed from the other table when this happens. These tables remove all duplicates. I also want Player Y to be able to add their name to Tables just like Player X can.
- My voting system removes duplicate names, it removes Player X’s names from the other voting option, but if Player Y votes, Player X’s name is removed from ALL TABLES. In theory, this would also remove a “Player A” and “PLayer B” 's etc. names from the table.
How do I fix this? Here’s the code:
local card1 = {} --Card is the name of the button that lets players vote
local card2 = {}
local card3 = {}
local event = game.ReplicatedStorage.Events.Vote -event fires when a player presses a voting option
local function vote(c, p) --c means card, for card1, card2, card3 tables. p is short for player.
local count = 0
table.insert(c, p) --adds plr to list
for i, v in c do --removes dupes to prevent double-voting
if v == p then
count += 1
if count > 1 then
table.remove(c, i)
end
end
end
end
event.OnServerEvent:Connect(function(plr, card) --when button is pressed
print("vote taken") --this prints successfully
if card == "Card1" then
print("Map1 voted") --this prints successfully
vote(card1, plr)
table.remove(card2, table.find(card2, plr))
table.remove(card3, table.find(card3, plr))
elseif card == "Card2" then
print("map2 voted") --this prints successfully
vote(card2, plr)
table.remove(card3, table.find(card3, plr))
table.remove(card1, table.find(card1, plr))
elseif card == "Card3" then
print("map3 voted") --this prints successfully
table.insert(card3, plr.Name)
vote(card3, plr)
table.remove(card2, table.find(card2, plr))
table.remove(card1, table.find(card1, plr)) --removes player from card 1, similarly above.
end
print("Map 1 has: ".. #card1 .. " voters")
print("Map 2 has: ".. #card2 .. " voters")
print("Map 3 has: ".. #card3 .. " voters")
end)
In a LocalServer, if Player1 votes Map 1 (Card 1) then it prints that Map 1 has 1 vote and the others have none. If Player1 presses Map 1 more than once, it still has 1 vote.
If Player1 presses Map2, Map1 gets 0 votes while Map2 gets 1, and Map3 gets 0 votes.
If Player2 presses any of the maps, then the map that they voted gets 1 vote and the others are reset to zero. This happens even Player2 votes for an option that Player1 has voted for.
Any help? I can’t figure out what’s wrong.