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:

--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

Log in to vote
2
Answered by 10 years ago

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
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