'attempt to index nil with X' that occurs 1/10 times?

I thought originally it was things loading to quick but after adding a :WaitForChild() I got an infinite yield.
Here’s the code.

if not newTemplate:FindFirstChild("Reference") then
		local ref = Instance.new("ObjectValue")
		ref.Name = "Reference"
		ref.Parent = newTemplate
	end
	newTemplate:WaitForChild("Reference").Value = player
	newTemplate.Parent = leaderboardContainer
	newTemplate.Frame.Title.Text = player.Name
newTemplate.Frame.Title.Text = player.Name

This line right here is the issue, as you can see above:

if not newTemplate:FindFirstChild("Reference") then
		local ref = Instance.new("ObjectValue")
		ref.Name = "Reference"
		ref.Parent = newTemplate
	end

I was having this issue with the an object value.

It’s cloning a template which contains all of these things yet there’s times where it doesn’t exist? I don’t get it.

Try creating a local ref variable outside everything else

local ref

if not newTemplate:FindFirstChild("Reference") then
    ref = Instance.new("ObjectValue")
    ref.Name = "Reference"
    ref.Parent = newTemplate
end

if ref then
    ref.Value = player
else
    print("No valid reference has been found")
end

newTemplate.Parent = leaderboardContainer
newTemplate.Frame.Title.Text = player.Name