You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I’m trying to make a voting system, but a problem I’m facing is that if someone with the most votes leaves before they win the voting, my script breaks. -
What is the issue? Include screenshots / videos if possible!
I made a playerRemoving event and if the player is found in the mostvotes table, it removes them from the table, but the problem I’m facing is that it wont find it in the table.find
--ServerScriptService Script
local function electDictator()
while true do
votinginprogress.Value = true
local newDictator = nil
repeat wait() until #players:GetPlayers() > 0
Dictator.Value = nil
print("Voting has started!")
alreadyVoted = {}
mostvotes = {}
players.PlayerRemoving:Connect(function(player)
if player == Dictator.Value then
dictatorleft.Value = true
Dictator.Value = nil
CloseGuiEvent:FireAllClients("DictatorMessagesGui")
status.Value = "Dictator has left the server!"
wait(3)
status.Value = ""
end
--Relevant part of the script
if table.find(mostvotes, game.ServerStorage.Candidates:FindFirstChild(tostring(player.UserId))) then
table.remove(mostvotes,table.find(mostvotes,game.ServerStorage.Candidates:FindFirstChild(tostring(player.UserId)))) --Doesn't find the player's intvalue in the folder
print("Removed player from this table")
else
print("player wasnt in mostvotes table")
end
end)
VoteEvent:FireAllClients(true)
--Create the Candidates folder and the IntValues with the Player's UserId as the name
local Candidates = Instance.new("Folder")
Candidates.Name = "Candidates"
Candidates.Parent = game.ServerStorage
for i, player in pairs(players:GetPlayers()) do
local playerInt = Instance.new("IntValue")
playerInt.Name = player.UserId
playerInt.Value = 0
playerInt.Parent = Candidates
end
VoteEvent.OnServerEvent:Connect(function(player, playerVoted)
print("Someone voted!")
if not table.find(alreadyVoted,player) then
table.insert(alreadyVoted, player)
print("This player hasn't voted before!")
for i, playerInt in pairs(Candidates:GetChildren()) do
if playerInt.Name == tostring(playerVoted.UserId) then
playerInt.Value += 1
print("Candidate " .. playerVoted.Name .. " has been voted for!")
end
end
else
print(player.Name .. " has already voted!")
end
end)
for i = 10, 1, -1 do
status.Value = "Voting ("..i..")"
wait(1)
end
dictatorleft.Value = false
for i, playerInt in pairs(Candidates:GetChildren()) do
if mostvotes[1] ~= nil then
if mostvotes[1].Value < playerInt.Value then
mostvotes = {}
table.insert(mostvotes, playerInt)
print("Candidate with more votes has been picked")
elseif mostvotes.Value == playerInt.Value then
table.insert(mostvotes, playerInt)
print("Candidate has the same amount of votes as num 1!")
end
else
table.insert(mostvotes, playerInt)
print("No picked candidate yet, setting player as num 1")
end
end
VoteEvent:FireAllClients(false)
votinginprogress.Value = false
if #mostvotes > 1 then
Dictator.Value = players:GetPlayerByUserId(tonumber(mostvotes[#mostvotes].Name))
print("Picking random candidate from mostvotes to become Dictator!")
else
Dictator.Value = players:GetPlayerByUserId(tonumber(mostvotes[1].Name))
print(mostvotes[1].Name)
print("New Dictator picked!")
end
makeDictatorNameTag(Dictator.Value)
status.Value = Dictator.Value.Name .. " is the new Dictator!"
sfx.CheeringSound:Play()
mostvotes = {}
alreadyVoted = {}
Candidates:Destroy()
wait(5)
status.Value = ""
for i = 30, 1, -1 do
if dictatorleft.Value == true then
break
end
TimerInt.Value = i
print(i)
wait(1)
end
print("restarting loop")
end
end
Also if you find a way to improve my script, please tell me as it will help me a lot!