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.
The issue comes from these lines table.find is returning nil, meaning you are basically doing
table.remove(card2, nil)
As the second argument is actually optional, it goes to the default of removing the first object, and hence other players’ votes are removed.
One way to fix this is to firstly check that the result of table.find is not nil. Another way is to adjust the whole script, which I will send in a second…
local cards = {{}, {}, {}}
local event = game.Replicatedstorage.Events.Vote
event.OnServerEvent:Connect(function(plr, card)
local ID = tonumber(string.sub(card, 5, 5)) -- get the card's number
print(`{card} voted!`)
for _, Card in Cards do
if table.find(Card, plr) then
table.remove(Card, table.find(Card, plr))
end
end
table.insert(cards[ID], plr)
print("Map 1 has: ".. #cards[1].. " voters")
print("Map 2 has: ".. #cards[2] .. " voters")
print("Map 3 has: ".. #cards[3] .. " voters")
end
edit: Realised I missed a line of code It has been updated
Making it only remove from the other tables if it isnt nil “table.find(etc etc) ~= nil” breaks the whole script and allows you to vote for multiple maps simultaneously
Modifying the existing- your script works perfectly. Thanks a ton!!
I have a question as to how it works, though:
What is ID? I get that it “gets the card’s number” but how does it do that? What happens if it isn’t there?
User @SeargentAUS found the solution to this, but his code had some errors so I’m posting the rectified version of it:
local cards = {{}, {}, {}}
local event = game.ReplicatedStorage.Events.Vote
event.OnServerEvent:Connect(function(plr, card)
local ID = tonumber(string.sub(card, 5, 5)) -- get the card's number
print(`{card} voted!`)
for _, Card in cards do
if table.find(Card, plr) then
table.remove(Card, table.find(Card, plr))
end
end
table.insert(cards[ID], plr)
print("Map 1 has: ".. #cards[1].. " voters")
print("Map 2 has: ".. #cards[2] .. " voters")
print("Map 3 has: ".. #cards[3] .. " voters")
end)
ID is basically just looking at the value of card, for example “Card1”, getting the 5th character of it, in this case “1”, and converting it to a number. I then use it as an index in the table