Weird leaderboard behaviour

  1. What do you want to achieve? Keep it simple and clear!
    I would like the leaderboard to display everyones data

  2. What is the issue? Include screenshots / videos if possible!
    The leaderboard works fine when alone, when a new player joins the older player can see both their data and the new players data but the new player can only see their own data and the old players data is just a dash.

Just wondering if anyone has had anything like this happen? or any ideas on why it might happen.

Ignore that post, read that wrong.

How do you update the data for each of the players?

Can you include your scripts is your post? We can’t help you if we can’t see your scripts

There are three scripts that load process and save the data

I’m not sure how to make the script appear right within the message so i appologise for that.

The first script loads data

local playerDataStore = require(script.Parent.PlayerDataStore)
local function onPlayerJoin(player)
local loadedData = playerDataStore.getDataForPlayer(player)
local leaderstats = player:FindFirstChild(“leaderstats”)
local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(Player)	
	
	-- make the leaderboard
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local exp = Instance.new("IntValue")
	local exp = player.leaderstats:FindFirstChild("Experience")
	exp.Value = 0
	exp.Name = "Experience"
	exp.Value = loadedData.exp
	exp.Parent = leaderstats
	
	local Bucks = Instance.new("IntValue")
	local Bucks = player.leaderstats:FindFirstChild("Bucks")
	Bucks.Value = 0
	Bucks.Name = "Bucks"
	Bucks.Value = loadedData.Bucks
	Bucks.Parent = leaderstats		
	
end)

end

game.Players.PlayerAdded:Connect(onPlayerJoin)

The second saves the data

– load in the PlayerDataStore module
local playerDataStore = require(script.Parent.PlayerDataStore)

local function onPlayerJoin(player)
– get the player’s information from the data store,
– and use it to initialize the leaderstats
local loadedData = playerDataStore.getDataForPlayer(player)

-- make the leaderboard
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local exp = Instance.new("IntValue")
exp.Name = "Experience"
exp.Value = loadedData.exp
exp.Parent = leaderstats

local Bucks = Instance.new("IntValue")
Bucks.Name = "Bucks"
Bucks.Value = loadedData.Bucks
Bucks.Parent = leaderstats

print(“Player data loaded”)
end

local function onPlayerExit(player)
– when a player leaves, save their data
local playerStats = player:FindFirstChild(“leaderstats”)
local saveData = {
exp = playerStats.Experience.Value,
Bucks = playerStats.Bucks.Value,
}
playerDataStore.saveDataForPlayer(player, saveData)
print(“Player Data Saved”)
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

The third proccess the data within a module
– Make a database called PlayerExperience, we will store all of our data here
local DataStoreService = game:GetService(“DataStoreService”)
local playerStore = DataStoreService:GetDataStore(“PlayerExperience”)

local defaultData = {
exp = 150,
rank = 0,
–coins = 0,
–rebirths = 0,
}
local PlayerDataStore = {}

function PlayerDataStore.getDataForPlayer(player)
– attempt to get the data for a player
local playerData
local success, err = pcall(function()
playerData = playerStore:GetAsync(player.UserId)
end)

-- if it fails, there are two possibilities:
--  a) the player has never played before
--  b) the network request failed for some reason
-- either way, give them the default data
if not success or not playerData then
	print("Failed to fetch data for ", player.Name, " with error ", err)
	playerData = defaultData
else
	print("Found data : ", playerData)
end

-- give the data back to the caller
return playerData

end

function PlayerDataStore.saveDataForPlayer(player, saveData)
– since this call is asyncronous, it’s possible that it could fail, so pcall it
local success, err = pcall(function()
– use the player’s UserId as the key to store data
playerStore:SetAsync(player.UserId, saveData)
end)
if not success then
print(“Something went wrong, losing player data…”)
print(err)
end
end

return PlayerDataStore

Please indent your code properly so we can read

As i mentioned in the above post i do not know how to indent or format the scripts so they appear properly within the post.