Leaderboard doesn't show

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

I will try to add a “print” at the end of each sections, and let you know.

I tried adding “print” to the sections, but it’s not printing, maybe because the script needs to have the leaderboard to save data.

I changed the function leaderboardSetUp to playerAdded, but it’s still not working.

Actually, my leaderstats use to work about 1 day or 2 days before, but eversince I added another “IntValue” to the leaderstats, the leaderstats stopped showing.

Right: I really need you to both re-review my post and apply some basic debugging. If none of these are working out for you then you need to supply more information than just “it doesn’t work”. I showed you boilerplate that you could convert your code over to. I can convert that further:

local Players = game:GetService("Players")

local function playerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

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

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

Is there more in your script that’s happening that isn’t being shown? I need full details or a full code sample to work from. leaderstats show up fine for me.

1 Like