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
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
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