Table.find won't find a value in my if statement

You can write your topic however you want, but you need to answer these questions:

  1. 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.
  2. 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! :+1:

1 Like

Why do you put all of this inside a while loop? It will run indefinitely and could really exhaust the script. Also, you try connecting an event (multiple events, actually) inside the loop. Every time the loop runs, a new function will be connected to those events, and when the event fires, all of those functions will run at the same time. Basically, your script fires the PlayerRemoving function infinite times.

I suggest removing that while loop, as I do not see why it’s there in the first place.

As for the table.find, first you should check if the player’s IntValue actually exists. Maybe the script can’t find it because it’s not there, or it’s named incorrectly.
Second, comparing instances to instances is a bit sketchy here. If the IntValue is named after the player’s UserId, then you could just check if the IntValue’s name is player.UserId. I also suggest saving the UserId in the “mostvotes” table instead of the actual instances, as that would be more reliable (but having the instances in the table is not a mistake or fail, if it works then it’s alright).

for index, value in pairs(mostvotes) do
	if value.Name == tostring(player.UserId) then
		table.remove(mostvotes, i)
		print("Removed player from this table")
		break
	end
end

This is how I would check and remove the player that left. Don’t forget to also destroy the IntValue that the player has after leaving.
I hope this helps!

2 Likes

I’m doing the while true loop because I want the function to run infinitely.
Should I remove the while true loop and make a seperate while true loop, something like this:

while true do
electDictator()
end

But here you say that it would exhaust the script, so How can I make it run indefinitely without it exhausting the script?

1 Like

Oh so you want the function to continue running until the server stops?

Then you can leave the while loop, just make sure there are enough wait()s in the script that it doesn’t run multiple times a second.

Yes, this would be a better approach. Or simply just purring all the code inside the loop. But I suggest keeping it in a unique function so it can be accessed easier later.

1 Like