ScreenGui Child nil when player dies

Whenever a player dies, the console will return an error saying that one of the ScreenGUI is nil. I want to fix this issue because I am creating a death screen and I need the death screen to consistently appear even when the player is dead. So far the first few lines of code work but after some lines, an error just abruptly appears out of nowhere.

while true do
	if player.Character:WaitForChild("Humanoid").Health == 0 then
		local i
		game.Players[player.Name].PlayerGui.DeathGUI.Enabled = true
		game.Players[player.Name].PlayerGui.DeathGUI.rame.Fame.Message.Text = msg[math.random(1, 14)]
		game.Players[player.Name].PlayerGui.DeathGUI.rame.Fame.menu.Activated:Connect(function()
			player.Team = Teams.Menu
			game.Players[player.Name].PlayerGui.DeathGUI.Enabled = false
			game.Players[player.Name].leaderstats.Type.Value = ""
			game.ReplicatedStorage.ResetCharacter:FireServer()
			game.ReplicatedStorage.MovePlayerToSpawn:FireServer()
			game.ReplicatedStorage.DestroyHumanoid:FireServer()
		end)	
		for i = 10, 0, -1 do
			DeathGUI:FindFirstChild("rame"):FindFirstChild("Fame").Count.Text = i -- this line is the error, rame and fame are Frame objects
			wait(1)
			if (i == 0) then
				player.Team = Teams.Menu
				game.Players[player.Name].PlayerGui.DeathGUI.Enabled = false
				game.Players[player.Name].leaderstats.Type.Value = ""
				game.ReplicatedStorage.ResetCharacter:FireServer()
				game.ReplicatedStorage.MovePlayerToSpawn:FireServer()
				game.ReplicatedStorage.DestroyHumanoid:FireServer()
			end
		end
	end
	wait(0.4)
end

Here is the error:

 12:19:51.307 - Players.bootsareme.PlayerScripts.DeathScreenHandler:21: attempt to index nil with 'FindFirstChild'
12:19:51.309 - Stack Begin
12:19:51.312 - Script 'Players.bootsareme.PlayerScripts.DeathScreenHandler', Line 21
12:19:51.313 - Stack End

I suspect that once a player dies, their PlayerGui resets therefore rendering the Frame as nil, but I want the death screen to persist so I can’t have the death screen be nil.

Have you tried setting the ScreenGUI’s property “ResetOnSpawn” to false?

2 Likes