TL;DR: I need help finding a better way to do my election system that doesn’t revolve around remote events.
I’ve just been having issues with my horribly tangled up scripts and would like to know better alternatives. Whilst this might sound like a post for #help-and-feedback:code-review, it isn’t; the code isn’t functioning how I’d like it.
For each player in the game, the code fires once more(due to the remote events, of course), but this causes deep issues. The code is for an election system, so each time it repeats, it changes the candidates. now imagine this occurring per each player in the server, and you’ll see the issue. Anyways, here is the script(in order of localscripts to serverscripts, as I cannot put them in chain of command due to my own personal confusion, despite being the person behind them).
electionstarter(all of these scripts will make more sense upon seeing the serverscripts, I promise.):
local presidenteTeam = game.Teams["El presidente"]
local civ = game.Teams.Civilian
local eorl = game:GetService("ReplicatedStorage").eorl
while true do
wait(51)
if #civ:GetPlayers() >= 1 then
if #presidenteTeam:GetPlayers() == 0 then
eorl:FireServer()
end
end
end
clientvote:
local votec = game.ReplicatedStorage:FindFirstChild("voteclient")
local player = game:GetService("Players").LocalPlayer
local elc = game.ReplicatedStorage:FindFirstChild("Election")
local function onClientEventFired()
print("Hello")
wait(1)
player.PlayerGui.election.Enabled = true
task.wait(10)
player.PlayerGui.election.Enabled = false
elc:FireServer()
end
votec.OnClientEvent:Connect(onClientEventFired)
election1:
local elac = game:GetService("ReplicatedStorage"):FindFirstChild("ELECTOROROR1")
local value = game.Workspace.ELECTIONRUNNING
local pres = game:GetService("Teams")["El presidente"]
local client2 = game:GetService("ReplicatedStorage"):FindFirstChild("client2")
local function onClientEventFired()
print("222")
wait(30)
local player = game:GetService("Players").LocalPlayer
local str = game.Workspace.votemodel.part.SurfaceGui.TextLabel.Text
if game.Workspace.vote.Value > game.Workspace.vote2.Value then
if str:find(player.Name) then
elac:FireServer()
end
end
end
client2.OnClientEvent:Connect(onClientEventFired)
election2:
local elac = game:GetService("ReplicatedStorage"):FindFirstChild("eldotor2")
local value = game.Workspace.ELECTIONRUNNING
local pres = game:GetService("Teams")["El presidente"]
local client1 = game:GetService("ReplicatedStorage"):FindFirstChild("client1")
local function onClientEventFired()
print("222")
wait(30)
local player = game:GetService("Players").LocalPlayer
local str = game.Workspace.votemodel2.part.SurfaceGui.TextLabel.Text
if game.Workspace.vote2.Value > game.Workspace.vote.Value then
if str:find(player.Name) then
elac:FireServer()
end
end
end
client1.OnClientEvent:Connect(onClientEventFired)
and now to the serverscripts!
SEPERATEgot(goes with the first localscript, I believe.):
local elec = game.Workspace.election
local eorl = game:GetService("ReplicatedStorage").eorl
eorl.OnServerEvent:Connect(function(player)
elec.Value = true
end)
election2serverside:
local pres = game:GetService("Teams"):FindFirstChild("El presidente")
local elac = game:GetService("ReplicatedStorage"):FindFirstChild("eldotor2")
elac.OnServerEvent:Connect(function(player)
local str = game.Workspace.votemodel2.part.SurfaceGui.TextLabel.Text
if game.Workspace.vote2.Value > game.Workspace.vote.Value then
if str:find(player.Name) then player.Team = pres
end
end
end)
election1serverside:
local pres = game:GetService("Teams"):FindFirstChild("El presidente")
local elac = game:GetService("ReplicatedStorage"):FindFirstChild("ELECTOROROR1")
elac.OnServerEvent:Connect(function(player)
local str = game.Workspace.votemodel.part.SurfaceGui.TextLabel.Text
if game.Workspace.vote.Value > game.Workspace.vote2.Value then
if str:find(player.Name) then player.Team = pres
end
end
end)
seperatevote:
local presidenteTeam = game.Teams["El presidente"]
local civilian = game.Teams.Civilian
local Players = game:GetService("Players")
local votec = game.ReplicatedStorage:FindFirstChild("voteclient")
local elec = game.Workspace.election
elec.Value = true
while true do
wait(4)
if elec.Value == true then
if #presidenteTeam:GetPlayers() == 0 then
if #civilian:GetPlayers() > 1 then
print("election")
local sound = Instance.new("Sound")
sound.Parent = game:GetService("SoundService")
sound.SoundId = "rbxassetid://9071337529"
sound:Play()
votec:FireAllClients()
sound.Ended:Wait()
sound:Destroy()
elec.Value = false
end
end
end
end
and now, the final serverscript(which causes issues due to how it is fired by each player, thus re-doing the candidate names):
local elc = game.ReplicatedStorage:FindFirstChild("Election")
local client1 = game:GetService("ReplicatedStorage"):FindFirstChild("client1")
local client2 = game:GetService("ReplicatedStorage"):FindFirstChild("client2")
elc.OnServerEvent:Connect(function(player)
print("fir")
local Players = game.Players:GetPlayers()
local nplrs = #Players
local Randomplayer = nil
local Randomplayer2 = nil
if nplrs > 0 then
Randomplayer = Players[math.random(1, nplrs)]
Randomplayer2 = Players[math.random(1, nplrs)]
end
if Randomplayer.Name == Randomplayer2.Name then
repeat Randomplayer = Players[math.random(1, nplrs)]
until Randomplayer.Name ~= Randomplayer2.Name
end
game.Workspace.votemodel.part.SurfaceGui.TextLabel.Text = Randomplayer.Name
game.Workspace.votemodel2.part.SurfaceGui.TextLabel.Text = Randomplayer2.Name
client1:FireAllClients()
client2:FireAllClients()
end)