In my script below, when a player dies, a line is supposed to be printed stating that the player has died. It seems pretty simple, and I thought that I did everything right until I tried it out multiple times and it didn’t work every single time. Here is my code below, anyone see any issues? *Something to note is that every player spawns in as a cube. The cube is a model that I named HumanoidRootPart and inserted it into a model called StarterCharacter under StarterPlayer as a Parent. I don’t know if this affects anything though…
This script is in StarterPlayerScripts btw.
game:GetService('Players').PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
if humanoid.Health <= 0 then
print(player.Name .. " has died!")
end
end)
end)
end)
Here is a player’s children in the Workspace when they enter and play the game:
Why is the script in StarterPlayerScripts? Move it to ServerScriptService and make it a regular script, or just do this if it must be local
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
print(player.Name .. " has died!")
end)
Your player is almost always added before the event has a chance to detect you if you use a localscript, but at this point you can just properties
PlayerAdded has no time to detect if your localplayer has been added if it’s in a localscript, you need to use something like how I had mentioned with my example
Your script worked, but now when I try to detect when the player has respawned, it doesn’t work. Here is my code below:
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
print(player.Name .. " has died!")
cubecolor = tostring(player.Character:WaitForChild("HumanoidRootPart").BrickColor)
print(cubecolor)
end)
player.CharacterAdded:Connect(function(character)
wait()
print(cubecolor)
if cubecolor ~= nil then
character:WaitForChild("HumanoidRootPart").BrickColor = BrickColor.new(cubecolor)
end
end)
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local cubecolor
local function death()
print(player.Name .. " has died!")
cubecolor = tostring(character:WaitForChild("HumanoidRootPart").BrickColor)
print(cubecolor)
end
humanoid.Died:Connect(death)
player.CharacterAdded:Connect(function(char)
wait()
print(cubecolor)
if cubecolor ~= nil then
char:WaitForChild("HumanoidRootPart").BrickColor = BrickColor.new(cubecolor)
end
character = char
humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(death)
end)
I believe it could because it’s working 1 time since those character and humanoid variables only work for that current session alive and or your character parameter overlapping with the variable? Also forgot to ask, does it at least print cubecolor?