How to check if there is a value in a table?

I am working on a round/wave-based game right now and I am trying to use an if not-do code block. I want to see if there is not any value present in a table then, do the following; however, I don’t know how to use the table.find(table, value) in the if not-do statement in a way where it checks for any value rather than a specific value.

In laments terms, there is a table with players who havent died yet in it. At the start of every round I want to check and see if there are any players left in the table or not, and if not start the waves over at 1, else re-add everyone to the list and move onto the next wave.

Here is my code:

if not table.find(_G.gameplayers, ANY VALUE) then
		for i, v in pairs(game.Players:GetPlayers()) do
			if v then
				table.insert(_G.gameplayers, v.Name)
				print(""..v.Name.." added to table")
			end
		end
		server_Wave.Value = 1
		print("No Survivors, resetting waves.")
	else
		for i, v in pairs(game.Players:GetPlayers()) do
			if v then
				if not table.find(_G.gameplayers, v.Name) then
					table.insert(_G.gameplayers, v.Name)
				else
					print("Player already a survivor.")
				end
			end
		end
		server_Wave.Value = server_Wave.Value + 1
		print("Survivors found, continuing waves.")
	end

Thanks for the help!

I believe all you need to utilize is the # operator. Check out this post!

1 Like

Are you using table.remove to get rid of the players in the table when they die? If so, you can just do

if #_G.gameplayers==0 then
    server_Wave.Value = 1
    for i,v in ipairs(game.Players:GetPlayers()) do
        table.insert(_G.gameplayers,v)
    end
end
1 Like