Leaderboard not working

I’m working on a game and I have a basic leader board that gets added to the player when they enter the game. It was working fine and then suddenly the instances stopped working. If someone could help with this it would be much appreciated.

Here’s the code:

game.Players.PlayerAdded:Connect(function(player)

local LS = Instance.new("Folder")
LS.Name = "leaderstats"
LS.Parent = player

local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = LS

end)

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    

    local points = Instance.new("IntValue", leaderstats)
    points.Name = "Points"
end)

Hi! The code you’ve provided works, except rather replace first line with

game:GetService("Players").PlayerAdded:Connect(function(player)

This code belongs in server script (Script) placed in ServerScriptService.

PS: a lot of people use Instance.new(“Folder”, player), and it is acceptable in this case, but don’t use instance with second argument otherwise if you care about performance. Your way of parenting last is longer to write but ideal. @XxFirelordxX12 @BlueMinionIW

Here an easy way to make leader stats.

`game.Players.PlayerAdded:Connect(function(player)

local LS = Instance.new(“Folder”, player)
LS.Name = “leaderstats”

local Points = Instance.new(“IntValue”, leaderstats)
Points.Name = “Points”
end)`

The way you and him did it does not matter because it’s the same thing but made the line of code a bit different.

“Player” remains an unknown variable. You need to add a PlayerAdded event (meaning when players join the game) in order for the script to know where tonput the folder.

game.Players.PlayerAdded:Connect(function(player)

You are right, it doesn’t matter in this case, but it does a lot if you have multiple properties to change. Once you parent a defined instance, multiple game processes start listening for changes, so the difference in performance is pretty big if you are spawning a lot of instances. Parent goes last when determining a lot of properties.

@Nkk2356 Please read what I wrote again. I didn’t say you can’t do that. Of course you can because Instance.new() accepts two arguments. I said it’s not a good practice if you set a lot of custom properties after. You have three ways to create an intance, and the most performance friendly one is the one without second argument. Parenting last is the best. However, this case doesn’t require changing a lot of properties afterwards, so it’s good enough to write Instance.new(“Intvalue”, player).

No because it’s still going to show up on the leaderboard the same as he did it the way he did.