Instance.new issue I've never seen

I kept getting the error “attempt to index nil with ‘WaitForChild’” and other methods included FindFirstChild and Nil etc until I actually looked at what was going on and I saw this.

image

Not really sure what the issue is, here’s the script.

---- leaderboard stats ----

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player) -- Parented to the player
	leaderstats.Name = "leaderstats"
	
	local hiddenstats = Instance.new("Folder", player)
	hiddenstats.Name = "hiddenstats"
	
	local clicks = Instance.new("IntValue", leaderstats)
	clicks.Name = "Clicks"
	clicks.Value = 0
	
	local rebirths = Instance.new("IntValue", hiddenstats)
	rebirths.Name = "Rebirths"
	rebirths.Value = 0
	
	local prestige = Instance.new("IntValue", hiddenstats)
	rebirths.Name = "Prestige"
	rebirths.Value = 0
	
	local ascend = Instance.new("IntValue", hiddenstats)
	rebirths.Name = "Ascend"
	rebirths.Value = 0
	
	local transend = Instance.new("IntValue", hiddenstats)
	rebirths.Name = "Transcend"
	rebirths.Value = 0
	
end)

1 Like

Seems like you’re reusing the rebirth variable to change the values. Try this:

---- leaderboard stats ----

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player) -- Parented to the player
	leaderstats.Name = "leaderstats"
	
	local hiddenstats = Instance.new("Folder", player)
	hiddenstats.Name = "hiddenstats"
	
	local clicks = Instance.new("IntValue", leaderstats)
	clicks.Name = "Clicks"
	clicks.Value = 0
	
	local rebirths = Instance.new("IntValue", hiddenstats)
	rebirths.Name = "Rebirths"
	rebirths.Value = 0
	
	local prestige = Instance.new("IntValue", hiddenstats)
	prestige.Name = "Prestige"
	prestige.Value = 0
	
	local ascend = Instance.new("IntValue", hiddenstats)
	ascend.Name = "Ascend"
	ascend.Value = 0
	
	local transend = Instance.new("IntValue", hiddenstats)
	transend.Name = "Transcend"
	transend.Value = 0
	
end)
Extra info

I saw a post saying that you should prefer not to use the second argument of the Instance.new.
Just parent it separately, like:

local value = Instance.new("IntValue")
value.Parent = hiddenstats

Oh wow, I didn’t even see that! I just copy pasted the rebirth value and changed the names. I am so blind.

Blockquote I saw a post saying that you should prefer not to use the second argument of the Instance.new .
Just parent it separately

Why is that?

Nothing much, it mostly causes performance-side issues. Although if you want a more detailed explanation:

PSA: Don’t use Instance.new() with parent argument - Updates / Announcements - DevForum | Roblox

1 Like

Ty for that, it was very helpful.

1 Like

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