Leaderboard doesn't show

Hello, I followed a tutorial, and wrote a leaderstats script, but when I join in my game, the leaderstats does not show.

I checked the script multiple times, but I can’t find any mistakes.
There are no errors or warnings in the output.

This is the script:

    local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = data or 0
	money.Parent = leaderstats

	print("Leaderstats loaded.")

What am I doing wrong? Thank you.

1 Like

please show the whole script, thanks

1 Like

You never got the player and what is data? Is this a local or via server script?

1 Like

they obv defined them if they have no errors

Here is part of the script.

local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")

local dataStore = dataStoreService:GetDataStore("Name")

local function leaderboardSetUp(player)
	local userId = player.UserId
	local key = "Player_" .. userId
	local data = dataStore:GetAsync(key)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = data or 0
	money.Parent = leaderstats

	print("Leaderstats loaded.")

end

Thanks.

This is a script from ServerScriptService.

did you forgot to add this?

players.PlayerAdded:Connect(leaderboardSetUp)

I added this script at the bottom of the script.

players.PlayerAdded:Connect(leaderboardSetUp)

You’ve probably run into an infamous race condition that will surely be more prominent in the future which has to do with running a yielding function before PlayerAdded connects. I encourage you, as well as any other developer, to apply the following boilerplate when working with PlayerAdded:

local Players = game:GetService("Players")

local function playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end

Where you originally write your PlayerAdded code you’d be writing into the function on the variable playerAdded. This boilerplate connects the given function to PlayerAdded and additionally runs the function for any players who joined the server before the PlayerAdded function connected.

An additional nitpick but please do make sure to parent your objects after you set their properties. It is also important that you pcall DataStore methods. If there is a DataStore outage, your script may either fail or cause data loss. If players still have data but the endpoints are not available you don’t handle this case of error and that can be catastrophic for your game in the long run. Last important thing is to use a table if you intend to store more than just cash - this current paradigm may lead you into creating more DataStores and keys for different data which will present you a different issue relating to overstepping DataStore limits.

1 Like

So do I add this

instead of this?

players.PlayerAdded:Connect(leaderboardSetUp)

Thanks.

Yes, though of course replace playerAdded with leaderboardSetUp. I don’t know if you run additional code so there may be other adjustments you need to make depending on your system. I just provided general boilerplate to avoid running into this race condition/yielding oversight.

1 Like
table.foreach(Players:GetPlayers(), function(_, p) -- change it to a for loop if u want
  playerAdded(p)
end)

Player.PlayerAdded:Connect(playerAdded)
1 Like

what is the point of this exactly?
it would literally run once when the game starts, and this would be useless because the server script would run that code before any player was added to the :GetPlayers() table

The leaderboard script was in a datastore script, and I never experienced this before. Thank you for helping me. :grinning:

there’s a chance people joining the game before the server starts, so looping existing players is a great idea

That’s the whole point of that piece of code. If you have a yielding function before a function can be connected to PlayerAdded then its connection time will be deferred. Any player who connects to the server ahead of the PlayerAdded connection registering will not have the function run for them, so that piece of code is intended to run the function on players who did not have it run before.

I never knew this was ever a possibility, good thing to know

I added this, but the leaderstats still won’t show.

could you try printing data, maybe that is a problem?

1 Like

Mm, please feel free to re-review my post.

As shown here:

1 Like