I need help with a gui showing on death

So, i have tried making a gui show on death and its not working at all
i get a error that goes by this: “ServerScriptService.Death:4: attempt to index nil with ‘Humanoid’”
i have tried to fix it and dont know why this doesn’t work
heres the script (in ServerScriptService) also i have a gui in the script called “DeathScreen”

game.Players.PlayerAdded:Connect(function(player)
	
	local char = player.Character
	local hum = char.Humanoid
hum.Died:Connect(function()
		local gui = script.DeathScreen:Clone()
		gui.Parent = player.PlayerGui
		local sound = Instance.new("Sound")
		sound.Volume = 0.5
		sound.SoundId = "rbxassetid://6629890936"
		sound.RollOffMaxDistance = 60
		sound.Parent = char.Head
		sound.Playing = true
		wait(7)
		sound:Remove()
		gui:Remove()
	end)
end)

use local char = player.Character or player.CharacterAdded:Wait()

maybe because the actual character player isn’t loaded yet

Your code will only run once
Use it:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		hum.Died:Connect(function()
			local gui = script.DeathScreen:Clone()
			gui.Parent = player.PlayerGui
			local sound = Instance.new("Sound")
			sound.Volume = 0.5
			sound.SoundId = "rbxassetid://6629890936"
			sound.RollOffMaxDistance = 60
			sound.Parent = char.Head
			sound.Playing = true
			wait(7)
			sound:Remove()
			gui:Remove()
		end)
	end)
end)

that is because the “old” humanoid is simply dead and nonexistent anymore you should use player.CharacterAdded:Connect (function(char) char:WaitForChild("Humanoid").Died and so on

so when you get respawned, basically you got a new body. so characteradded will do just that

there was no error but the gui didnt show up

yea , there’s nothing wrong there. just when your character died, basically you respawned with new character body and new humanoid

oh yea that explains it! thanks for the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.