How to check if a table is empty?

I am making a script for a game system and when the table is empty I want the game to end. But for some reason it doesn’t work.

local alivePlrs = {}

while true do
	wait(5)
	for i, plr in pairs(game.Players:GetPlayers()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(11.2, 0.5, 52.62)

		table.insert(alivePlrs, plr)
		print(alivePlrs)
		-- Story Starts Here
		plr.Character.Humanoid.Died:Connect(function()
			table.remove(alivePlrs, alivePlrs[plr])
			print(alivePlrs)
			if (next(alivePlrs) == nil) then -- This too
				workspace.Events.EndGame:Fire()
			end
		end)
	end
	wait(100)
	game.Players.PlayerRemoving:Connect(function(plr)
		print(plr.Name)
		table.remove(alivePlrs, alivePlrs[plr])
		print(alivePlrs)
		wait(0.5)
		if (next(alivePlrs) == nil) then -- This part here
			workspace.Events.EndGame:Fire()
		end
	end)
end

workspace.Events.EndGame.Event:Connect(function()
	print("Game Over")
	workspace["Ahh, fresh meat!"]:Play()
	alivePlrs = {}
	return
end)

You’re never skipping to the next iteration of your round loop when the table is empty. Hint: you can do this easily using continue, which skips back to the top of the loop.

I don’t get it, I put a continue anywhere and it just puts errors

Just by reading the title, this is pretty much how you check if a table is empty:

if #t < 1 then -- or "== 0"
end

I have tried that and it does nothing

function isEmpty(t)
	for i, v in t do
		return false
	end
	return true
end

Also, why the wait(100) before connecting PlayerRemoving?

it ain’t working, also the wait(100) is so that the script doesn’t repeat too fast, i removed it and it does no difference

For me the function works fine, how are you testing if the function works?

function isEmpty()
	for i, v in alivePlrs do
		if #alivePlrs == nil then
			workspace.Events.EndGame:Fire()
		end
		return false
	end
	return true
end


while true do
	wait(5)
	for i, plr in pairs(game.Players:GetPlayers()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(11.2, 0.5, 52.62)
		table.insert(alivePlrs, plr)
		print(alivePlrs)
		plr.Character.Humanoid.Died:Connect(function()
			table.remove(alivePlrs, alivePlrs[plr])
			print(alivePlrs)
			isEmpty()
		end)
	end
end

like that, idk if im doing it wrong the exact thing you put just gives an error

isEmpty() returns a boolean if the table is empty, it doesn’t bind to any game logic. You should do this

if isEmpty(alivePlrs) then
	--logic if there are no more alive players
end

Also you modified my function

So essentially you would want this instead

function isEmpty(t)
	for i, v in t do
		return false
	end
	return true
end

while true do
	wait(5)
	for i, plr in pairs(game.Players:GetPlayers()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(11.2, 0.5, 52.62)
		table.insert(alivePlrs, plr)
		print(alivePlrs)
		plr.Character.Humanoid.Died:Connect(function()
			table.remove(alivePlrs, alivePlrs[plr])
			if isEmpty(alivePlrs) then
				workspace.Events.EndGame:Fire()
			end
		end)
	end
end

Oh yeah it works now, another problem is the event function thing was at the bottom so I moved it to the top and it works.

I suggest to instead of looping through all results, checking the size of the table this will work with arrays and dictionaries.

function isEmpty(t)
   return #t == 0 -- if there are 0 values then it's empty 
end

Else you could check for the first value, this would only work for arrays.

function isEmpty(t)
   return t[1] == nil -- First value is not there.
end

The only problem with this code is that the game will crash after 30 minutes due to memory leaks:

That was the code he originally had, I was just modifying a part of it and sending him back the full thing.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.