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

Help With Death Detection And Team Switch?

Asked by
Scootakip 299 Moderation Voter
8 years ago
while true do
    if game.Players.LocalPlayer.Character.Humanoid.Health == 0 then
        game.Players.LocalPlayer.TeamColor = game.Teams["Dead"].TeamColor
    end 
    wait()
end

This is supposed to switch someone to team "Dead" when the game detects their health to be 0. It doesn't work. It's in a local script as well, someone help?

1 answer

Log in to vote
2
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Problem

Your script is inefficient constantly checking if the player's health is 0. This is unnecessary and if you have too many repeating loops, you might end up lagging your game.


Solution

You can actually use the Died event of Humanoid. This script really should be put in a server script as it deals with the changing of teams.

With the died event, we want to establish PlayerAdded and CharacterAdded events as well.

game.Players.PlayerAdded:connect(function(Player) --Player is the person who just joined the game, we can use the Player instance to help detect if a new Character was created.
    Player.CharacterAdded:connect(function(Character) --At this point the player has respawned and now we can access the Character through the Character variable.
        Character:WaitForChild('Humanoid').Died:connect(function() --At this point the player's character has died, now we can change the player's TeamColor. For this we can use the Player variable we established in the first function.
            Player.TeamColor = game.Teams.Dead.TeamColor
        end)
    end)
end)

Hopefully this helped, if it did leave an upvote and if it answered your question do not forget to hit accept answer! If you have any questions or if the script produces an error feel free to comment below.
Ad

Answer this question