Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer
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)

1 comment

0

MAKE SURE TO PUT IT IN STARTER CHARACTER FOLDER!

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)

5 comments

0

make sure its in the StarterCharacterScripts folder so the script loads in when a player respawns

0

LocalPlayer will never be nil and new characters will always be inside workspace.

0

Ohhh ok tysm

0

I was putting the script in start player

0

@cabbler just a habit I have when I make local scripts :p

This discussion is preserved as it was. New answers, comments, and votes are closed.