Leaderboard doesn't show

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

Here is the full script:

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

local dataStore = dataStoreService:GetDataStore("Name")

local function playerAdded(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

local function save(player)
	local userId = player.UserId
	local key = "Player_" .. userId
	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		local moneyValue = leaderstats.Money.Value
		local success, ret = pcall(dataStore.SetAsync, dataStore, key, moneyValue)

		if success then
			print("Data has been saved successfully!")
		else
			print("There was an error with saving a player's data" .. ret)
		end
	end
end

local function onShutDown()
	wait(1)
end
game:BindToClose(onShutDown)

while true do
	wait(60)
	for _,player in ipairs(players:GetPlayers()) do
		coroutine.wrap(save)(player)
	end
end


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

friendly reminder that if you put a loop before the playeradded, it will never run

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

local dataStore = dataStoreService:GetDataStore("Name")

local function playerAdded(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

local function save(player)
	local userId = player.UserId
	local key = "Player_" .. userId
	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		local moneyValue = leaderstats.Money.Value
		local success, ret = pcall(dataStore.SetAsync, dataStore, key, moneyValue)

		if success then
			print("Data has been saved successfully!")
		else
			print("There was an error with saving a player's data" .. ret)
		end
	end
end

local function onShutDown()
	wait(1)
end
game:BindToClose(onShutDown)

spawn(function()

while true do
	wait(60)
	for _,player in ipairs(players:GetPlayers()) do
		coroutine.wrap(save)(player)
	end
end

end)

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

Is there any difference compared to my script?

yes, your script has a loop before the player added event, spawn makes it doesn’t break

1 Like

bro you didn’t take the time to indent that

Yeah, there’s your issue. You have a non-terminating loop that’s running before PlayerAdded gets connected. Loops prevent code from running until they finish and in your case you have a loop that doesn’t ever end so the code under it is never reached.

Good thing I asked for more detail. Put that at the bottom instead.

@p49p0 Spawn is evil.

1 Like

lets talk abt this in dms, don’t talk here until we get flagged

Do you mean

while true do
	wait(60)
	for _,player in ipairs(players:GetPlayers()) do
		coroutine.wrap(save)(player)
	end
end

this?

he means, put the loop under the playeradded event and the player removing event

Thank you for your help! The leaderstats shows now. :grinning:

1 Like

Also, I want to give credit to @GEILER123456 for providing a tutorial for how to make a datastore script. I followed his tutorial.