I’ve looked on yt and devforum but can’t find anything to learn how to make a player voting system. Yes, I did look at map voting system, and I don’t think that’s the same because with map voting system, the variables/maps are already prewritten.
So how do I make a voting system? And check if anything ties?
I think I have an idea on how to do it, although I don’t know how it can be scripted.
What my idea would be either for people to stand on places to submit a vote, and make a script that counts and detects how many people are on the place, and then have that further use that number to determine which is the larger number and then apply that larger number’s vote into reality.
Or, a GUI could do that and detect and count the clicks, and use that to determine the larger number and apply the larger vote into reality.
fire an event to all players that will cause a gui with image buttons for each player in a list, then when the player clicks on another player it will send an event to the server with the userid of the player they chose
the server will keep two dictionaries, one with the userid and a bool value of whether or not they’ve already voted, and another with the userid and then the amount of votes they received
after a timer runs out or the players all vote, the server will go through the votes dictionary and pick the highest voted, if there is a tie, choose a random player out of the tied users
Yeah my plan is to use GUI. I’ve thought of the idea but I’m not sure how to continue, using RemoteEvents to communicate client-server.
My idea is to make a playerlist, but I don’t know how to count the players in there. @kylerzong For the dictionary, what do I use as key? And how do I sort the highest vote/number in a dictionary?
You would use the userid as the key
and to get the highest vote you loop through the dictionary, comparing each # with a variable of the highest vote, so if # > highest then highest = #
well if theres a tie that means multiple people have the same number, so you take the highest number and check who all has it, then put all them in a table and pick random
local VoteRemote = Instance.new("RemoteEvent")
VoteRemote.Name = "VoteRemote"
VoteRemote.Parent = game.ReplicatedStorage
while wait(5) do
local Voters = game.Players:GetPlayers()
if #Voters <= 2 then
print("Waiting for players")
local Event = Instance.new("BindableEvent")
Event.Event:Connect(function()
Voters = game.Players:GetPlayers()
end)
JoinConnection = game.Players.PlayerAdded:Connect(function(Plyr)
if #game.Players:GetPlayers() >= 3 then
Event:Fire()
JoinConnection:Disconnect()
end
end)
Event.Event:Wait()
Event:Destroy()
continue
end
print("Starting Vote")
for PlyrIndex = 1, #Voters do
VoteRemote:FireClient(Voters[PlyrIndex], Voters)
end
local Votes = {}
local Voted = {}
VoteConnection = VoteRemote.OnServerEvent:Connect(function(VoteSender, VotedUserId)
if table.find(Voters, VoteSender) == nil then return end
if VoteSender.UserId == VotedUserId then return end
if not Voted[VoteSender] then
Voted[VoteSender] = true
if Votes[VotedUserId] == nil then
Votes[VotedUserId] = 1
else
Votes[VotedUserId] += 1
end
end
end)
wait(10)
VoteConnection:Disconnect()
print("Voting Ended")
print(Votes)
local Highest = {0, nil}
local PossibleTie = false
for i = 1, #Voters do
if Votes[Voters[i].UserId] ~= nil then
if (Votes[Voters[i].UserId] or 0) > Highest[1] then
Highest = {Votes[Voters[i].UserId], Voters[i]}
end
end
end
local Tied = {}
for i = 1, #Voters do
if Votes[Voters[i].UserId] == Highest[1] then
table.insert(Tied, Voters[i])
end
end
if #Tied > 1 then
print("There was a tie.")
Highest[2] = Tied[math.random(1, #Tied)]
end
if Highest[1] == nil or Highest[2] == nil then
print("No Winner.")
else
print("Vote Winner: "..Highest[2].Name.." with "..Highest[1].." votes.")
end
end
Client (sends random vote)
local VoteRemote = game.ReplicatedStorage:WaitForChild("VoteRemote")
VoteRemote.OnClientEvent:Connect(function(Voters)
local Found = table.find(Voters, game.Players.LocalPlayer)
if Found ~= nil then
table.remove(Voters, Found)
end
if #Voters >= 1 then
print("Client -- Sending Random Vote")
VoteRemote:FireServer(Voters[math.random(1, #Voters)].UserId) -- Auto vote a random
end
end)