I’m trying to incorporate a re-voting system within my main voting system(that picks a specific player with the most votes as Manager) and it works locally to the player that triggers the re-voting process but it throws a error when trying to show a Gui up for the other players in the server.
I thought about trying to use a remote event to get a table of all the playerGuis then loop through each of them and turn on the Enabled property for the VotingGui screengui. Thanks in advance!!
Voting Manager (Local Script)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local voteRemote = ReplicatedStorage:WaitForChild("VoteEvent")
local reVoteRemote = ReplicatedStorage:WaitForChild("Revote")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:FindFirstChild("PlayerGui")
local VotingUI = PlayerGui:WaitForChild("VotingGui")
local VotingFrame = VotingUI:WaitForChild("Image").VotingFrame
-- Function: update voting buttons dynamically
local function updateVotingButtons()
-- Clear existing buttons
for _, child in pairs(VotingFrame:GetChildren()) do
if child:IsA("TextButton") then
child:Destroy()
end
end
-- Create buttons for each player
for _, player in ipairs(Players:GetPlayers()) do
local button = Instance.new("TextButton")
button.Size = UDim2.new(1, 0, 0.1, 0) -- Adjust size to fit
button.Text = player.Name
button.Font = Enum.Font.SourceSansBold
button.TextSize = 24
button.BackgroundColor3 = Color3.new(0.2, 0.2, 0.2)
button.TextColor3 = Color3.new(1, 1, 1)
button.Parent = VotingFrame
-- Handle button clicks
button.MouseButton1Click:Connect(function()
print("Voted for:", player.Name)
voteRemote:FireServer(player.Name)
-- Hide the voting GUI after voting
VotingUI.Enabled = false
end)
end
end
-- Function: handle re-voting
local function manageRevoting()
local Sidebar = PlayerGui:WaitForChild("Sidebar")
local RevoteButton = Sidebar.Container.ContainerTop.ImageLabel.Revote
RevoteButton.MouseButton1Click:Connect(function()
Sidebar.RevoteFrame.Visible = not Sidebar.RevoteFrame.Visible
Sidebar.RevoteFrame.Container.TriggerButton.MouseButton1Click:Connect(function()
Sidebar.RevoteFrame.Visible = false
for _, player in ipairs(Players:GetPlayers()) do
if player ~= nil then else warn("ERRRORRORORORO") end
local playerGui = player:FindFirstChild("PlayerGui")
print(playerGui.Name)
end
reVoteRemote:FireServer()
updateVotingButtons()
end)
end)
end
-- Update buttons when players join or leave
Players.PlayerAdded:Connect(updateVotingButtons)
Players.PlayerRemoving:Connect(updateVotingButtons)
-- Initial setup
updateVotingButtons()
manageRevoting()
Vote Handler (Normal Script)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RevoteEvent = ReplicatedStorage:WaitForChild("Revote")
-- Create or retrieve RemoteEvents for voting and role updates
local voteEvent = ReplicatedStorage:FindFirstChild("VoteEvent")
if not voteEvent then
voteEvent = Instance.new("RemoteEvent")
voteEvent.Name = "VoteEvent"
voteEvent.Parent = ReplicatedStorage
end
local roleEvent = ReplicatedStorage:FindFirstChild("RoleEvent")
if not roleEvent then
roleEvent = Instance.new("RemoteEvent")
roleEvent.Name = "RoleEvent"
roleEvent.Parent = ReplicatedStorage
print("DEBUG: RoleEvent created in ReplicatedStorage")
else
print("DEBUG: RoleEvent found in ReplicatedStorage")
end
-- Voting variables
local votes = {} -- Tracks votes for each player
local votingInProgress = false
-- Reset votes for all players
local function resetVotes()
votes = {}
for _, player in pairs(Players:GetPlayers()) do
votes[player.Name] = 0
end
end
-- Notify players of their roles
local function notifyRoles()
for _, player in pairs(Players:GetPlayers()) do
local role = player:FindFirstChild("Role")
if role then
roleEvent:FireClient(player, role.Value)
end
end
end
-- Tally votes and determine the manager
local function tallyVotes()
local maxVotes = 0
local manager = nil
for playerName, voteCount in pairs(votes) do
if voteCount > maxVotes then
maxVotes = voteCount
manager = playerName
end
end
-- Assign roles
for _, player in pairs(Players:GetPlayers()) do
if player.Name == manager then
player.Role.Value = "Manager"
else
player.Role.Value = "Associate"
end
end
print("Manager is:", manager)
notifyRoles() -- Notify clients of their updated roles
end
-- Handle player votes
voteEvent.OnServerEvent:Connect(function(voter, nomineeName)
if votingInProgress and votes[nomineeName] then
votes[nomineeName] = votes[nomineeName] + 1
print(voter.Name .. " voted for " .. nomineeName)
else
print("Voting not in progress or invalid nominee")
end
end)
-- Start the voting process
local function startVoting()
resetVotes()
votingInProgress = true
print("Voting has started!")
-- End voting after 30 seconds
task.delay(30, function()
votingInProgress = false
tallyVotes()
print("Voting has ended!")
end)
end
-- Start revoting proccess
local function startReVoting(_)
resetVotes()
votingInProgress = true
warn("Re-voting has started!")
-- End voting after 30 seconds
task.delay(30, function()
votingInProgress = false
tallyVotes()
print("Voting has ended!")
end)
end
RevoteEvent.OnServerEvent:Connect(startReVoting)
-- Automatically start voting when enough players are present
Players.PlayerAdded:Connect(function(player)
-- Ensure the player has a role
local role = player:FindFirstChild("Role")
if not role then
role = Instance.new("StringValue")
role.Name = "Role"
role.Value = "Associate" -- Default role
role.Parent = player
end
-- Start voting when 6 players are in the server
if #Players:GetPlayers() == 1 and not votingInProgress then
startVoting()
end
end)
-- Handle player leaving
Players.PlayerRemoving:Connect(function(player)
if votingInProgress and votes[player.Name] then
votes[player.Name] = nil
end
end)