Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does this event only listen once?

Asked by
Klamman 220 Moderation Voter
8 years ago

I'm having a lot of trouble with a script in my game:

01game.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)
16end)

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.

1 answer

Log in to vote
1
Answered by 8 years ago

I think it has to do with static variables.

Basically, the character can change. Once it dies, it re-spawns a new Character. This might be the reason this is happening.

How can I fix this?

Use a CharacterAdded event. Like so,

01game.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)
18end)
The Character Added Event Fires Everytime the player's character Respawns, providing a variable of the character.

I hope I helped.

Good Luck!

Ad

Answer this question