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

How could I detect when a player dies?

Asked by 4 years ago

Trying to make a script that puts 2 people into an arena and then they fight. I want to know how to:

  1. detect the player has died
  2. teleport both players after one has died

thanks in advance :)

3 answers

Log in to vote
-1
Answered by 4 years ago
if game.Players.LocalPlayer.Character.Humanoid.Health == 0 or < 0 then
    game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(0 0 0) --coordinates here
end

I may be wrong since I am not the best scripter but I hope this helps.

Ad
Log in to vote
0
Answered by 4 years ago

This should work. It first fires when a player is added, then the CharacterAdded event fires when the character is added, and finally the Humanoid.Died event fires when the player dies. When they die, the HumanoidRootPart is teleported to a position. Put this script inside ServerScriptStorage and make it a regular script/ServerScript. Try this:

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()
            character.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0) -- Change (0, 0, 0) to the position you want.
        end)
    end)
end)

Also, please note that this is NOT a request site. I see that you only have 6 reputation, so you must be pretty new. The next time you make a post, include code that you tried to make.

Log in to vote
0
Answered by
DesertusX 435 Moderation Voter
4 years ago
Edited 4 years ago

You can use Humanoid.Died. Prepare a part you want the players to get teleported to.

local players = game.Players:GetPlayers()
local teleportPart = "TeleportPart" --Change this to the name of your part.
local char1 = players[1].Character or players[1].CharacterAddded:Wait()
local char2 = players[2].Character or players[2].CharacterAddded:Wait()
local hum1 = char1.Humanoid
local hum2 = char2.Humanoid

hum1.Died:Connect(function()
    print(players[1].Name.." has died.")
    print(players[2].Name.." has won.")
    char1:MoveTo(workspace(teleportPart).Position)
    char2:MoveTo(workspace(teleportPart).Position)
end)

hum2.Died:Connect(function()
    print(players[2].Name.." has died.")
    print(players[1].Name.." has won.")
    char1:MoveTo(workspace(teleportPart).Position)
    char2:MoveTo(workspace(teleportPart).Position)
end)

My sincere apologies if this does not work for you.

Please do not hesitate to ask me if you have any questions or problems.

Don't forget to accept the answer if I helped!

Answer this question