Attempt to index nil with 'WaitForChild'

I’ve been trying to fix this index nil error for the past 14 minutes, still can’t fix it though, I even tried to find solutions from other threads but that did not help either…
Any help is appreciated!

The script:
(It’s also located in StarterPlayerScripts)

local player = game.Players.LocalPlayer
local char = player.Character
local h = char:WaitForChild("Humanoid")
local humanoid = char:FindFirstChild(h)
local info = TweenInfo.new(2, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
local tween =  game:GetService("TweenService"):Create(player["Death Message"], info,{BackgroundTransparency = 0})
local deathmessagelist = {"Me personally, i would not let that slide..", "Damn", "Did you know you that you have rights?", "jamesderrman would not like that..", "Kur would be proud!", player.Name..", Honestly you suck", "I am sort of a more mean guiding light!"}

if humanoid then
	humanoid.Died:Connect(function()	
		local gui = player.PlayerGui["Death Screen"]
		gui.Enabled = true
		gui.Frame.TextLabel.Text = deathmessagelist[math.random(1, #deathmessagelist)]
		tween:Play()
		tween.Completed:Connect(function()
			gui.Frame.TextLabel.Visible = true
		end)
		wait(9)
		gui.Frame.TextLabel = "[insert death message here]"
		gui.Frame.BackgroundTransparency = 1
	end)
end

The error:
image

2 Likes

It’s basically saying char is nil. You have to wait for it.

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

EDIT:
Also, remove the 4th line and instead of changing what I said before, just move the script to StarterCharacterScripts so that the script runs each time the player spawns instead of just once when the player joins the game.

4 Likes

Oh thanks it works now!
(Sorry for the late reply btw, I fell asleep on my keyboard)
((just realized I used the wrong word… This hit me like a train.))

2 Likes