I'm having a lot of trouble with a script in my game:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | wait( 1 ) |
03 | game.ReplicatedStorage.UpdateStatus:FireAllClients() |
04 | player.Character.Humanoid.Died:connect( function () |
05 | print ( "HUMANOID DIED" ) |
06 | local playerNum |
07 | for i, plr in pairs (players:GetPlayers()) do |
08 | if plr = = player.Name then |
09 | playerNum = i |
10 | print (player.Name .. "leaving game." ) |
11 | end |
12 | end |
13 | table.remove(playersPlaying, playerNum) |
14 | print (player.Name .. "leaving game." ) |
15 | end ) |
16 | end ) |
Can someone explain to me why the .Died event only seems to run once? To my knowledge, it should run like this: player joins, event's connected. Whenever the player dies from that point on, the code will run. Is this not the case? If so, can someone explain why? Help is much appreciated.
Basically, the character can change. Once it dies, it re-spawns a new Character. This might be the reason this is happening.
Use a CharacterAdded
event. Like so,
01 | game.Players.PlayerAdded:connect( function (player) |
02 | plr.CharacterAdded:connect( function (char) |
03 | wait( 1 ) --I would also maybe remove this |
04 | game.ReplicatedStorage.UpdateStatus:FireAllClients() |
05 | char.Humanoid.Died:connect( function () |
06 | print ( "HUMANOID DIED" ) |
07 | local playerNum |
08 | for i, plr in pairs (players:GetPlayers()) do |
09 | if plr = = player.Name then |
10 | playerNum = i |
11 | print (player.Name .. "leaving game." ) |
12 | end |
13 | end |
14 | table.remove(playersPlaying, playerNum) |
15 | print (player.Name .. "leaving game." ) |
16 | end ) |
17 | end ) |
18 | end ) |
I hope I helped.
Good Luck!