What do you want to achieve?
To understand and fix the script relating to Humanoid.Died.
What is the issue?
After dying the first time, Humanoid.Died doesn’t function properly anymore.
This server script is located inside the ServerScriptService.
game.Players.CharacterAutoLoads = false
game.Players.PlayerAdded:Connect(function(plr)
local deathinstance = Instance.new("IntValue")
deathinstance.Name= ("Death")
deathinstance.Parent = plr
deathinstance.Value = 0
plr:LoadCharacter()
print("player added")
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
local val = Instance.new("ObjectValue",workspace.AlivePlayers)
val.Name = plr.Name
val.Value = plr
local hum = plr.Character:WaitForChild("Humanoid")
hum.Died:connect(function()
local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
plr.Death.Value += 1
plr.PlayerGui.DeathScreen.DeathImpact:Play()
plr.PlayerGui.DeathScreen.DeathImpact2:Play()
plr.PlayerGui.DeathScreen.DeathAmbience:Play()
if plr.Death.Value == 1 then
wait(2)
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
plr.PlayerGui.DeathScreen.DeathImpact:Stop()
plr.PlayerGui.DeathScreen.DeathImpact2:Stop()
plr.PlayerGui.DeathScreen.RespawnImpact:Play()
plr.PlayerGui.DeathScreen.Respawning.Visible = true
plr.PlayerGui.DeathScreen.RespawningSound:Play()
local randomtext = math.random(1,4)
if randomtext == 1 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
elseif randomtext == 2 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
elseif randomtext == 3 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
elseif randomtext == 4 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
end
wait(2)
for i = 1,10 do
plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
wait()
end
wait(3.5)
plr:LoadCharacter()
else
print("remove")
vale:Destroy()
end
return
end)
end)
What solutions have you tried so far?
I have tried adding a CharacterAdded function, but it breaks the script even more so.
I have tried looking on the developer forums, but couldn’t find a solution.
Thank you for taking the time to read this post! Have a nice day!
To understand what’s going on here, it’s important to visualize this from a character perspetive. Each time a character resets or a humanoid dies, a new one is created. This means that your player added connection up top only tracks the first ever character. Let’s fix that by wrapping the hum.Died:Connect() with a plr.CharacterAdded:Connect() signal. It would look something like this:
Hey Beetee! Thank you for helping me out!
Unfortunately, when I added the CharacterAdded function below the PlayerAdded function, the character never loaded in, so I moved plr:LoadCharacter() in between both functions. The character now loads in, but whenever the player dies, the whole script doesn’t work.
This is what I have now currently:
game.Players.CharacterAutoLoads = false
game.Players.PlayerAdded:Connect(function(plr)
plr:LoadCharacter()
plr.CharacterAdded:Connect(function(character)
local deathinstance = Instance.new("IntValue")
deathinstance.Name= ("Death")
deathinstance.Parent = plr
deathinstance.Value = 0
print("player added")
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
local val = Instance.new("ObjectValue",workspace.AlivePlayers)
val.Name = plr.Name
val.Value = plr
local hum = plr.Character:WaitForChild("Humanoid")
hum.Died:connect(function()
local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
plr.Death.Value += 1
plr.PlayerGui.DeathScreen.DeathImpact:Play()
plr.PlayerGui.DeathScreen.DeathImpact2:Play()
plr.PlayerGui.DeathScreen.DeathAmbience:Play()
if plr.Death.Value == 1 then
wait(2)
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
plr.PlayerGui.DeathScreen.DeathImpact:Stop()
plr.PlayerGui.DeathScreen.DeathImpact2:Stop()
plr.PlayerGui.DeathScreen.RespawnImpact:Play()
plr.PlayerGui.DeathScreen.Respawning.Visible = true
plr.PlayerGui.DeathScreen.RespawningSound:Play()
local randomtext = math.random(1,4)
if randomtext == 1 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
elseif randomtext == 2 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
elseif randomtext == 3 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
elseif randomtext == 4 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
end
wait(2)
for i = 1,10 do
plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
wait()
end
wait(3.5)
plr:LoadCharacter()
else
print("remove")
vale:Destroy()
end
return
end)
end)
end)
Setting the :LoadCharacter() after the CharacterAdded function doesn’t properly load the character in. The player is added, but not the character itself.
I see the issue. You are doing :WaitForChild() to get the humanoid of the character. However, the character has yet to load, which causes infinite yield. Go ahead and wrap everything inside CharacterAdded in a
Hey Beetee! I tried adding the spawn function into the script, but the player still doesn’t load unfortunately.
game.Players.CharacterAutoLoads = false
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(character)
task.spawn(function()
local deathinstance = Instance.new("IntValue")
deathinstance.Name= ("Death")
deathinstance.Parent = plr
deathinstance.Value = 0
print("player added")
plr:LoadCharacter()
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
local val = Instance.new("ObjectValue",workspace.AlivePlayers)
val.Name = plr.Name
val.Value = plr
local hum = plr.Character:WaitForChild("Humanoid")
hum.Died:connect(function()
local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
plr.Death.Value += 1
plr.PlayerGui.DeathScreen.DeathImpact:Play()
plr.PlayerGui.DeathScreen.DeathImpact2:Play()
plr.PlayerGui.DeathScreen.DeathAmbience:Play()
if plr.Death.Value == 1 then
wait(2)
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
plr.PlayerGui.DeathScreen.DeathImpact:Stop()
plr.PlayerGui.DeathScreen.DeathImpact2:Stop()
plr.PlayerGui.DeathScreen.RespawnImpact:Play()
plr.PlayerGui.DeathScreen.Respawning.Visible = true
plr.PlayerGui.DeathScreen.RespawningSound:Play()
local randomtext = math.random(1,4)
if randomtext == 1 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
elseif randomtext == 2 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
elseif randomtext == 3 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
elseif randomtext == 4 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
end
wait(2)
for i = 1,10 do
plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
wait()
end
wait(3.5)
plr:LoadCharacter()
else
print("remove")
vale:Destroy()
end
end)
end)
end)
end)
Hey beetee!
After reworking my script similar to yours, it finally works! Thank you so much!
The script now:
game.Players.CharacterAutoLoads = false
game.Players.PlayerAdded:Connect(function(plr)
print("player added")
plr.CharacterAdded:Connect(function(character)
--task.spawn(function()
repeat wait() until plr.Character:WaitForChild("Humanoid")
local deathinstance = Instance.new("IntValue")
deathinstance.Name= ("Death")
deathinstance.Parent = plr
deathinstance.Value = 0
print("character added")
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
local val = Instance.new("ObjectValue",workspace.AlivePlayers)
val.Name = plr.Name
val.Value = plr
local hum = plr.Character:WaitForChild("Humanoid")
print("remove")
hum.Died:connect(function()
local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
plr.Death.Value += 1
plr.PlayerGui.DeathScreen.DeathImpact:Play()
plr.PlayerGui.DeathScreen.DeathImpact2:Play()
plr.PlayerGui.DeathScreen.DeathAmbience:Play()
vale:Destroy()
if plr.Death.Value == 1 then
wait(2)
plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
plr.PlayerGui.DeathScreen.DeathImpact:Stop()
plr.PlayerGui.DeathScreen.DeathImpact2:Stop()
plr.PlayerGui.DeathScreen.RespawnImpact:Play()
plr.PlayerGui.DeathScreen.Respawning.Visible = true
plr.PlayerGui.DeathScreen.RespawningSound:Play()
local randomtext = math.random(1,4)
if randomtext == 1 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
elseif randomtext == 2 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
elseif randomtext == 3 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
elseif randomtext == 4 then
plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
end
wait(2)
for i = 1,10 do
plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
wait()
end
wait(3.5)
plr:LoadCharacter()
end
--end)
end)
end)
plr:LoadCharacter()
end)