Is there a better way to go about this election system?

Basically, I’ve completed the bare-bones structure for the election system in my game, but before I go any further, I would like feedback on the current code. The first script originally had a onplayeradded function, but that would cause the player who joined to be unable to actually see the election GUI.
I’ve also noticed there’s a slight delay between when the GUIs appear for each player in this current rendition, which isn’t exactly what I’d like.
(P.S the “break” in the first script is just until I find a better solution)
Localscript that triggers the election:

local team = game.Teams["El presidente"]
local civilian = game.Teams.Civilian
local remoteEvent = game.ReplicatedStorage:FindFirstChild("vote")
local Players = game:GetService("Players")
while true do 
	wait(4)
	if #team:GetPlayers() == 0 then
		if #civilian:GetPlayers() == 2 then
			print("election")
			remoteEvent:FireServer()
			break
		end
	end
	end

Serverscript that gets fired by that localscript:

local votec = game.ReplicatedStorage:FindFirstChild("voteclient")
local vote = game.ReplicatedStorage:FindFirstChild("vote")
local sound = Instance.new("Sound", game.Workspace)
local presidente = game.Teams:FindFirstChild("El presidente")
sound.SoundId = "rbxassetid://9071337529"
vote.OnServerEvent:Connect(function(player)
	local Players = game:GetService('Players')
	wait(8)
		votec:FireClient(player)
		end)


localscript that is fired by the serverscript:

local votec = game.ReplicatedStorage:FindFirstChild("voteclient")
local player = game:GetService("Players").LocalPlayer
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9071337529"
local function onClientEventFired()
	print("Hello")
	player.PlayerGui.election.Enabled = true 
	sound:Play()
	task.wait(10)
	player.PlayerGui.election.Enabled = false
	player.PlayerGui.register.Enabled = true
	end

votec.OnClientEvent:Connect(onClientEventFired)

1 Like

why are you checking for players in a localscript? combine the first script with the second also if #civilian:GetPlayers() == 2 then replace == with >
edit: you can use FireAllClients instead of doing it with everyone individually unless you want some people to not vote which you can do in the gui instead
also why a break in the loop? you dont want more than 1 president in each server?
heres the first and 2nd script combined

local presidenteTeam = game.Teams["El presidente"]
local civilian = game.Teams.Civilian
local Players = game:GetService("Players")
local votec = game.ReplicatedStorage:FindFirstChild("voteclient")
local sound = Instance.new("Sound", workspace)

while true do 
	task.wait(4)
	if #presidenteTeam:GetPlayers() == 0 then
		if #civilian:GetPlayers() > 2 then
			print("election")
			sound.SoundId = "rbxassetid://9071337529"
			sound:Play()
			votec:FireAllClients()
			sound.Ended:Wait()
			sound:Destroy()
		end
	end
end
1 Like

Thanks for your help! Also, yeah, I only want one president per server. That’s why I added the break.

1 Like

the break will ensure that no one can become president when the first one leaves his team or the server not sure why you want this

I know that. It was just used a temporary solution, as I didn’t want to fire the remoteevent multiple times(as it was handled in the client). Due to your great idea to put it on the server I think it’s no longer necessary.

1 Like