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

Questions about tables?

I'm a bit new to Tables, and utilizing them. I've got a few questions. First of all, I want my script to check if a table is empty or not. Here is the code I have so far:

--I want this to check if the redTeam is dead, or if both the green AND blue team are BOTH dead.
if redTeam == "" then
		--code
	elseif greenTeam == "" and blueteam == "" then
		--code
	end

And my second question, how would I remove a player from a table if he dies? My attempted code:

function playerDied(humanoid)
	local player = humanoid.Parent.Parent
	if redTeam[player] then
		table.remove(redTeam,player)
	elseif greenTeam[player] then
		table.remove(greenTeam,player)
	elseif blueTeam[player] then
		table.remove(blueTeam,player)
	else
	end
end

Also, would the following efficiently call playerDied?

while true do
	for i,v in pairs(game.Players:GetChildren()) do
		v.Character:WaitForChild("Humanoid").Died:connect(playerDied)
	end
	wait()
end

1 answer

First question:

for i = 1,#RedTeam do
print(RedTeam[i].Name) -- will print the name of everyone in this table.
end 

Second question:

for a,b in pairs(TableName)
b.Character.Humanoid.Died:connect(function(playerDied)
end

function playerDied(p)
for a,b in pairs(TableName) do
if p.Name == b.Name then
table.remove(TableName,a)
end
end

3 comments

0

Your answer to my first question just states how to print the table's value. How could I check if it was empty?

0

Oh. I actually don't know you could do local numInTab = 0 for i = 1,#RedTeam do print(RedTeam[i].Name) numInTab = numInTab + 1 end if numInTab == 0 then print("No one in table") end

0

Yeah ^ That should work! Sorry for the confusion.

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