How to create a voting system? any ideas?

Hello, I would like to create a voting system through a gui, that each player can only vote 1 time in each option (3 options) and that it is a single vote per player, how can I do it? Someone could share commented code with me, I would really appreciate it very much for my learning :frowning:

I want to create something like this:

image

2 Likes

Maybe use math.random for the votes

Here is a sample:

local mapfolder = game.ReplicatedStorage.Maps:GetChildren()
local randommap = mapfolder[math.random(1, #mapfolder)]
print(randommap)
local clone = randommap:Clone()
clone.Parent = workspace

Edit: this was made in devforum

Hm, a unique approach

Well you should hopefully know how to get a basic understanding on these in order to make this work:

  • RemoteEvents

  • GuiButton Events

So what the first things we should do, is create a RemoteEvent inside ReplicatedStorage so that we’ll be able to detect its client/server communications whenever the Gui Button gets pressed

image

Next we’ll create out Map Votes inside ServerStorage, preferably a NumberValue/IntValue so that it’s secure & no one can be capable of changing it in case exploiters manage to do so

And what we can go ahead & do next, is reference our LocalScript inside our StarterGui service, individually referencing all of the Buttons:

local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Screen = PlayerGui:WaitForChild("ScreenGui")

local Button1 = Screen:WaitForChild("Vote1")
local Button2 = Screen:WaitForChild("Vote2")
local Button2 = Screen:WaitForChild("Vote3")

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

--All of these Events will send a RemoteEvent with a number parameter depending on which they voted on
Button1.MouseButton1Down:Connect(function()
    Event:FireServer(1)
end)

Button2.MouseButton1Down:Connect(function()
    Event:FireServer(2)
end)

Button3.MouseButton1Down:Connect(function()
    Event:FireServer(3)
end)

The next thing what we could do, is then create a ServerScript inside ServerScriptService

We should get all of our Map Vote Numbers so we can add onto that whenever a player first votes for a map, and getting our RemoteEvent from the server-side, we can detect if this specific player has voted or not by creating a BoolValue checking if the Player has voted or not:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local MapVotes = game.ServerStorage:WaitForChild("VoteOptions") --A folder for easier use

game.Players.PlayerAdded:Connect(function(Player)
    local Voted = Instance.new("BoolValue")
    Voted.Parent = Player --No need to set its value, cause by default its set to false
end)

Event.OnServerEvent:Connect(function(Player, VoteNumber)
    if Player.Voted == false then --This will check if the Player has already voted or not
        Player.Voted = true

        if VoteNumber == 1 then
            MapVotes.Map1.Value += 1

        elseif VoteNumber == 2 then
            MapVotes.Map2.Value += 1

        elseif VoteNumber == 3 then
            MapVotes.Map3.Value += 1
        end
    end
end)

Depending on what VoteNumber the player has fired, we can check if this player has voted to add onto the specific map’s value!

Of course, this is just a simple & broad example on how to implement it but if you have anymore questions do feel free to ask

1 Like

This is the way I did it, I have no idea how it works, I wrote the code at 3 am

		local finished = false
		local playersClicked = {}
		local qpwoe = completeChoice.OnServerEvent:Connect(function(p,choice)
			if playersClicked[p.Name] == true then return end
			playersClicked[p.Name] = true
			if pqowie == false then
				pqowie = true
				coroutine.wrap(function()
					repeat wait() until trackableTimer(5) == true
					if not firedClients then
						if choice1Num >= choice2Num then
							playersClicked = {}
							finished = true
							choice1Num = 0
							choice2Num = 0
							pFired = 0
							event.Choices.Choice1.Callback()
							choiceEvent:FireAllClients()
							canAdvanceDialogVariable = true
						else
							playersClicked = {}
							pFired = 0
							finished = true
							choice1Num = 0
							choice2Num = 0
							choiceEvent:FireAllClients()
							canAdvanceDialogVariable = true
							event.Choices.Choice2.Callback()
						end
					else
						firedClients = false
					end
				end)()
			end
			if choice:lower() == "choice1" then
				choice1Num = choice1Num + 1
			else
				choice2Num = choice2Num + 1
			end
		end)
		repeat wait() until finished == true
                firedClients = false
		isActiveChoice = false
		qpwoe:Disconnect()
1 Like