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

How do i make the script detect the player being dead?

Asked by
Plieax 66
6 years ago

This is what I have to make the script detect the players health constantly. But I dont know how to make it work over and over

local player = game.Players.LocalPlayer
local workplayer = game.Workspace:WaitForChild(player.Name)
local humanoid = workplayer.Humanoid
local remote = game.ReplicatedStorage.Playerhealth0

repeat wait()

until humanoid.Health <= 0
remote:FireServer()

2 answers

Log in to vote
0
Answered by 6 years ago
local player = game.Players.LocalPlayer
local workplayer = player.Character or player.CharacterAdded:Wait()
local humanoid = workplayer:WaitForChild("Humanoid")
local remote = game.ReplicatedStorage:WaitForChild("Playerhealth0")

humanoid.Died:Connect(function()
    remote:FireServer()
end)
0
MAKE SURE TO PUT IT IN STARTER CHARACTER FOLDER! Plieax 66 — 6y
Ad
Log in to vote
2
Answered by
hellmatic 1523 Moderation Voter
6 years ago

Instead of using a loop, use the humanoid.Died event.

Place this in StarterCharacterScripts under the StarterPlayer folder.

repeat wait() until game.Players.LocalPlayer
repeat wait until game.Players.LocalPlayer.Character:IsDescendantOf(workspace)
----------------------------------------------------------------------------------------------------------

local player = game.Players.LocalPlayer
local workplayer = game.Workspace:WaitForChild(player.Name)
local humanoid = workplayer.Humanoid
local remote = game.ReplicatedStorage:WaitForChild('Playerhealth0')


function ON_DEATH()
    remote:FireServer()
    print(player.Name .. " died")
end

humanoid.Died:connect(ON_DEATH)
0
make sure its in the StarterCharacterScripts folder so the script loads in when a player respawns hellmatic 1523 — 6y
0
LocalPlayer will never be nil and new characters will always be inside workspace. cabbler 1942 — 6y
0
Ohhh ok tysm Plieax 66 — 6y
0
I was putting the script in start player Plieax 66 — 6y
0
@cabbler just a habit I have when I make local scripts :p hellmatic 1523 — 6y

Answer this question