So I'm trying to make it so that my script adds players into a table, right. Now, every time a player dies, I want to remove it from that table. Now whenever I do this, it doesnt work.
01 | for i,v in pairs (Player) do |
02 | enabled = false |
03 | v.Character.Humanoid.Changed:connect( function () |
04 | if v.Character.Humanoid.Health = = 0 and enabled = = false then |
05 | enabled = true |
06 | print (v.Name.. " has been killed." ) |
07 | table.remove(Player, v) -- Says number expected, got object can u help with that |
08 | print (#Player) |
09 | if #Player = = 1 or #Player = = 0 then |
10 | Game_In_Progress = false |
11 | print ( "Game Has Ended" ) |
12 | end |
13 | end |
14 | end ) |
15 | end |
Try changing it to:
1 | -- code changing |
2 | If v.Character. Humanoid. Health = = 0 and enabled = = false then |
3 | enabled = true |
4 | print (v.Name.. " has been killed." ) |
5 | table.remove( -- Table name here, i -- You should use 'i' because you are at the position of the object you want to move) |
6 | print (#Player) |
7 | -- rest of code |
So what's happening is you're already inside the table looping through, so you won't find the "Player" really, you will find its position and you need to get rid of that. So it should be 'i' and not 'Player'.
Humanoid.Died fires whenever a humanoid's health reaches 0, so you can use that as a replacement. Also, table.remove's second argument is the index to "remove". You have to find the player that died's spot on the table (via loop or another method).
01 | for i,v in pairs (Player) do |
02 | enabled = false |
03 | v.Character.Humanoid.Died:connect( function () |
04 | if not enabled then |
05 | enabled = true |
06 | print (v.Name.. " has been killed." ) |
07 | for b,c in pairs (Player) do --assuming Player is a table |
08 | if v = = c then |
09 | table.remove(Player, b) |
10 | break |
11 | end |
12 | end |
13 | print (#Player) |
14 | if #Player < = 1 then |
15 | Game_In_Progress = false |
16 | print ( "Game Has Ended" ) |
17 | end |
18 | end |
19 | end ) |
20 | end |