Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I detect if a table is empty?

lets say this is the script:

local plrTable = {}
local plrTableNextPos = 0

game.Players.PlayerAdded:Connect(function(Player)

	plrTableNextPos = plrTableNextPos + 1
	table.insert(plrTable, plrTableNextPos, Player.Name)

end)
game.Players.PlayerRemoving:Connect(function(Player)

	plrTableNextPos = plrTableNextPos - 1
	table.remove(plrTable, plrTableNextPos)

end)

... simple, right? BUT, what if I wanted to print "Empty server" when the table is empty. How do I know? heres what im thinking but this wont work (i know because I tried):

local plrTable = {}
local plrTableNextPos = 0

game.Players.PlayerAdded:Connect(function(Player)

	plrTableNextPos = plrTableNextPos + 1
	table.insert(plrTable, plrTableNextPos, Player.Name)

end)
game.Players.PlayerRemoving:Connect(function(Player)

	plrTableNextPos = plrTableNextPos - 1
	table.remove(plrTable, plrTableNextPos)

end)

if plrTable == nil then -- detect if table is empty part

	print("Empty server")

end

how could i do this?

2 comments

1

You can use "next(table)", if it return "nil" then the table is nil, if return the first table element, then there is a player, other else, you can use "table[1] == nil", which just work for arrays.

0

thanks

1 answer

local amm = #plrTable or 0

if amm == 0 then
	-- Code
end

This discussion is preserved as it was. New answers, comments, and votes are closed.