|
| 05 Aug 2016 02:20 AM |
Okay so it prints all players have died if there is only one player testing, but with two players #myPlayers stays 1 even if they both die. Help? Thanks
local myPlayers = game.Players:GetPlayers()
for i,v in pairs(myPlayers) do --for each player if v.Character then v.Character.Humanoid.Died:connect(function() table.remove(myPlayers, i) if #myPlayers == 0 then print('All players have died!') end end) end end
|
|
|
| Report Abuse |
|
|
Obbay2
|
  |
| Joined: 23 Aug 2008 |
| Total Posts: 51 |
|
|
| 05 Aug 2016 02:38 AM |
table.remove shifts all values in the table when used. So to your second part if the "first" player dies first, there won't be any player at index 2 to remove.
As to why only one player causes the print I'm not sure. |
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 02:51 AM |
Oh okay, what should I use instead?
|
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 05 Aug 2016 11:38 AM |
So obviously the table.remove is the problem, does anyone know what else I could use?
|
|
|
| Report Abuse |
|
|
| |
|
|
| 05 Aug 2016 11:41 AM |
Yeah that's what I thought, I looked and there's nothing on tables that would help me with this. I wrote the whole script from reading the wiki and understanding it, but obviously I don't understand the table.remove part.
|
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 05 Aug 2016 12:21 PM |
| Well duh. You only use 1 loop which means they only die once and it counts them one time |
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 12:23 PM |
Do you know how I could fix it?
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 12:25 PM |
| use CharacterAdded for each player when they join and then Died |
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 12:28 PM |
Still kinda confused, something like this?
local myPlayers = game.Players:GetPlayers()
for i,v in pairs(myPlayers) do --for each player if v.Character then --CharacterAdded v.Character.Humanoid.Died:connect(function() table.remove(myPlayers, i) if #myPlayers == 0 then print('All players have died!') end end) end end
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 12:30 PM |
Read it in english, does that make sense?
For each player, if they have a character, when they die, connect function
you want:
when a player is added, when their character is added, when they die, connect function |
|
|
| Report Abuse |
|
|
| |
|
|
| 05 Aug 2016 01:11 PM |
Hm still doesnt end the game
for i,v in pairs(myPlayers) do --for each player --game.Players.PlayerAdded:connect(function(player) game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) if v.Character then --if they have a character v.Character.Humanoid.Died:connect(function() table.remove(myPlayers, i) if #myPlayers == 0 then game.Workspace.Map.EndGame.Value = true end end) end end) end) end
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 01:21 PM |
>when a player is added, when their character is added, when they die, connect function
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 01:23 PM |
game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(c) c:WaitForChild("Humanoid").Died:connect(function() print( "rip " .. p.Name ) end) end) end)
Very easy |
|
|
| Report Abuse |
|
|
| |
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 05 Aug 2016 01:37 PM |
That's not what he's asking for ^^
He wants it so it starts a game with all players in the server, then when one of them dies it removes them until there's nobody left. Something like this:
function startGame() local plrs = {} for i,v in pairs(game.Players:GetPlayers()) do plrs[v.Name] = v local dieded dieded = v.Character.Humanoid.Died:connect(function() plrs[v.Name] = nil dieded:disconnect() if #plrs == 0 then print 'uh oh every1 dieded' end end) end end |
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 01:42 PM |
@kodran Thanks that helped alot!
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 01:45 PM |
@kodran Im trying to solve it, but if theres two players in the game and if one resets the game ends. Hm
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 01:50 PM |
Do I need to combine what both of you guys said?
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2016 02:11 PM |
It works perfectly with one player. It ends the game and all. however with two people if only one player resets, the game ends even though one player is still alive
|
|
|
| Report Abuse |
|
|