|
| 01 Jun 2015 04:26 PM |
I'm having trouble with tables. How do you check if something is in a table?
game.Players.PlayerAdded:connect(function(plr) local plrz = plr.Name plr.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() if activeplayers.plrz then table.remove(activeplayers, plr.Name) else print("no game") end end) end) end)
My players name is in the table (I used print), but it keeps printing "no game" |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 01 Jun 2015 04:28 PM |
Since you're adding it as a value, you can't just check if it's there.
Rather than telling you to loop through the table, use a simpler solution.
local activeplayers = {}
game.Players.PlayerAdded:connect(function(plr) local plrz = plr.Name plr.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() if activeplayers[plrz] then activeplayers[plrz] = nil else print("no game") end end) end) end)
Don't do
table.insert(activeplayerz, plrz)
Do
activeplayers[plrz] = true
|
|
|
| Report Abuse |
|
|
| |
|
|
| 01 Jun 2015 04:34 PM |
How I insert:
for i,v in pairs(game.Players:GetPlayers()) do activeplayers[v] = true print("inserted "..v.Name.." into activeplayers table") v.Character.Torso.CFrame = spawns[math.random(#spawns)].CFrame v.Character.Humanoid.WalkSpeed = 0 end
game.Players.PlayerAdded:connect(function(plr) local plrz = plr.Name plr.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() if activeplayers.plrz then table.remove(activeplayers, plr.Name) end end) end) end)
This isn't removing ^ |
|
|
| Report Abuse |
|
|
| |
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 01 Jun 2015 04:40 PM |
You need to make sure you read things before trying them. I showed you to _not_ use table.remove. Do
activeplayers[plrz] = nil
Instead |
|
|
| Report Abuse |
|
|
|
| 01 Jun 2015 04:47 PM |
Okay so when I use
game.Players.PlayerAdded:connect(function(plr) local plrz = plr.Name plr.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() if activeplayers[plrz] then activeplayers[plrz] = nil else print("no game") end end) end) end)
This, automatically breaks the end?
for i = 45, 0, -1 do wait(1) print(table.concat(activeplayers, ' ')) status.Value = i if #activeplayers == 0 then break end end |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 01 Jun 2015 04:50 PM |
| You can't count non-numerical indices. The table is always 0. |
|
|
| Report Abuse |
|
|
|
| 01 Jun 2015 04:51 PM |
| How would I check if the table is empty then? I rarely use them |
|
|
| Report Abuse |
|
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 02 Jun 2015 08:02 AM |
function CountTable(Table) local X = 0 for i,v in pairs(Table) do X = X + 1; end return X end |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2015 09:38 AM |
local table = require(199207348)
if table.find(activeplayers, plrz) then --code end |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2015 09:40 AM |
local table = require(199207348)
if table.length(tab) > 0 then --not empty end |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2015 08:21 PM |
for i = 45, 0, -1 do wait(1) print(table.concat(activeplayers, ' ')) status.Value = i CountTable() if X == 0 then break end end
Okay so how do I see what CountTable returns? |
|
|
| Report Abuse |
|
|
|
| 02 Jun 2015 08:26 PM |
Also, I'm receiving this error,
20:24:46.812 - ServerScriptService.Mainscript:17: bad argument #1 to 'pairs' (table expected, got nil) |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 02 Jun 2015 09:06 PM |
"CountTable() if X == 0 then break end"
To
if CountTable(activeplayers) == 0 then break end
|
|
|
| Report Abuse |
|
|
|
| 02 Jun 2015 09:08 PM |
Awesome thanks.
So this isn't working though:
game.Players.PlayerAdded:connect(function(plr) local plrz = plr.Name plr.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() if activeplayers[plrz] then activeplayers[plrz] = nil else print("no game") end end) end) end)
The player is indeed, in the table, as through the print I used in the loop |
|
|
| Report Abuse |
|
|
|
| 03 Jun 2015 04:05 AM |
local table = require(199207348)
game.Players.PlayerAdded:connect(function(plr) local plrz = plr.Name plr.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() local found, value, index = table.find(activeplayers, plrz) if found then activeplayers[index] = nil else print("no game") end end) end) end) |
|
|
| Report Abuse |
|
|
murcury57
|
  |
| Joined: 30 Jun 2010 |
| Total Posts: 90299 |
|
| |
|