Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Questions about tables?

Asked by 10 years ago

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:

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

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

01function playerDied(humanoid)
02    local player = humanoid.Parent.Parent
03    if redTeam[player] then
04        table.remove(redTeam,player)
05    elseif greenTeam[player] then
06        table.remove(greenTeam,player)
07    elseif blueTeam[player] then
08        table.remove(blueTeam,player)
09    else
10    end
11end

Also, would the following efficiently call playerDied?

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

1 answer

Log in to vote
2
Answered by 10 years ago

First question:

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

Second question:

01for a,b in pairs(TableName)
02b.Character.Humanoid.Died:connect(function(playerDied)
03end
04 
05function playerDied(p)
06for a,b in pairs(TableName) do
07if p.Name == b.Name then
08table.remove(TableName,a)
09end
10end
0
Your answer to my first question just states how to print the table's value. How could I check if it was empty? SlickPwner 534 — 10y
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 ObscureEntity 294 — 10y
0
Yeah ^ That should work! Sorry for the confusion. ObscureEntity 294 — 10y
Ad

Answer this question