GUI won't show when player dies

So, I am trying to make a GUI show when a player dies, but instead of showing it brings up this error:
Players.MRBOGO_YT.PlayerScripts.DisplayScript.ScreenGui.Frame.LocalScript:143: attempt to index nil with 'Visible'
Code:

local Character = game.Workspace:WaitForChild(player.Name)

player.Character.Humanoid.Died:Connect(function()
	script.Parent.Visible = true
end)

Screenshot:
Screenshot 2021-07-19 10-36-47

The only thing that could cause it is if soemthing parents the frame to nil or uses Destroy on the frame or the ScreenGui, you may need to see if something is doing that.

Also, for

local Character = game.Workspace:WaitForChild(player.Name)

Just use

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

Edit: Also, why is the script in PlayerScripts if you have a ScreenGui?

just do…

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

also make sure its a local script.

1 Like

Because the player doesn’t have a character yet because they still have to set it so I make it wait until the player has a character.

The line I proposed is a better way of doing it, if the character doesn’t exist by the time the script runs and reaches that line, it’ll wait for the CharacterAdded event to respond which when ti does, will return the character. Also, take a look at what I said about it beign something that probably destroys the frame before the Visible line can work

1 Like

Working but now it doesn’t think the player has a humanoid when they , most certainly do. And it shows that the character has a Humanoid when I look in the workspace:


And I looked for the Destroy() line but all I had done in earlier scripts was make it invisible.

Since this is in PlayerScripts, You may probably need to update any references to the character when they respawn, what is the code in the erroring script?

1 Like

It is the same line of code. This is the line of code:

player.Character.Humanoid.Died:Connect(function()

Just like @EmbatTheHybrid mentioned, you may need to update your reference to the character when the player respawns. Since your LocalScript is in PlayerScripts, it will not reset once you die and your reference will still be on the old character.
Use CharacterAdded to define a new reference to the character once a new one has been added.

1 Like

Oh, okay. so how would I make the character respawn? because they don’t right now. That is because their skin is set to a new skin during gameplay.

You may need to wait for the humanoid because it might not have loaded yet:

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

Character:WaitForChild("Humanoid").Died:Connect(function()
	script.Parent.Visible = true
end)  

and you can turn ResetOnSpawn in the properties of the ScreenGui to false to prevent the gui from resetting itself:

image

Changed nothing. Here is the error: (And I do want it to reset on spawn)
11:21:44.252 Players.MRBOGO_YT.PlayerGui.ScreenGui.Frame.LocalScript:23: attempt to index nil with 'Humanoid' - Client - LocalScript:23

May I see more of your code please to help locate the issue?

Yes. This is the code to make all the GUI’s work: Local Script

script.Parent.Play.Visible = true
script.Parent.GameIcon.Visible = true
script.Parent.PlayAsCharacter.Visible = false
script.Parent.CharacterImage.Visible = false
script.Parent.SelectionBox.Visible = false
local CharacterSelected = false
local player = game.Players.LocalPlayer
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

script.Parent.Play.MouseButton1Click:Connect(function()
	script.Parent.Play.Visible = false
	script.Parent.GameIcon.Visible = false
	script.Parent.PlayAsCharacter.Visible = true
	script.Parent.CharacterImage.Visible = true
	script.Parent.SelectionBox.Visible = true
end)
script.Parent.SelectionBox.CarolineForbes.MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
			player.Character.Humanoid.Died:Connect(function()
				script.Parent.Visible = true
			end)
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["1"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
			player.Character.Humanoid.Died:Connect(function()
				script.Parent.Visible = true
			end)
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["2"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
			player.Character.Humanoid.Died:Connect(function()
				script.Parent.Visible = true
			end)
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["3"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
			player.Character.Humanoid.Died:Connect(function()
				script.Parent.Visible = true
			end)
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["5"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["6"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["7"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["8"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["9"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
		end)
	else
		CharacterSelected = false
	end
end)
script.Parent.SelectionBox["10"].MouseButton1Click:Connect(function()
	if CharacterSelected == false then
		CharacterSelected = true
		script.Parent.PlayAsCharacter.MouseButton1Click:Connect(function()
			RemoteEvent:FireServer()
			script.Parent.Visible = false
		end)
	else
		CharacterSelected = false
	end
end)

local Character = player.CharacterAdded:Wait()

player.Character:WaitForChild("Humanoid").Died:Connect(function()
	script.Parent.Visible = true
end)

The code to make the GUI visible: Local Script

local Player = game.Players.LocalPlayer
local GUI = script.ScreenGui:Clone()

GUI.Parent = Player:WaitForChild("PlayerGui")

local Character = Player.CharacterAdded:Wait()

Character.Humanoid.Died:Connect(function()
	GUI.Parent = Player:WaitForChild("PlayerGui")
end)

The code to make the character spawn: Server Script

local RemoteEvent = game.ReplicatedStorage.RemoteEvent --Change this to your RemoteEvent
game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:Connect(function(player)
	RemoteEvent.OnServerEvent:Connect(function()
		player:LoadCharacter()
		player.Character.Animate.Parent = game.ReplicatedStorage.StarterCharacter
		local character = game.ReplicatedStorage.StarterCharacter
		local characterClone = character:Clone()
		characterClone.Name = player.Name
		characterClone:MakeJoints()
		player.Character = characterClone

		characterClone.Parent = workspace
	end)
	local Character = player.CharacterAdded:Wait()
	
	Character.Humanoid.Died:Connect(function()
		player:LoadCharacter()
		player.Character.Animate.Parent = game.ReplicatedStorage.StarterCharacter
		local character = game.ReplicatedStorage.StarterCharacter
		local characterClone = character:Clone()
		characterClone.Name = player.Name
		characterClone:MakeJoints()
		player.Character = characterClone

		characterClone.Parent = workspace
	end)
end)

If resetonspawn is true then make sure to make it false. that is under the screengui object.

Still doesn’t work. It probably will work when we figure out how to fix the error message:
11:21:44.252 Players.MRBOGO_YT.PlayerGui.ScreenGui.Frame.LocalScript:23: attempt to index nil with 'Humanoid' - Client - LocalScript:23