How can I access a players datastore?

Hey Everyone,
So I’m making a leaderboard for my story game with stands, each with the NPC. I’m trying to make the npc show the wins amount. I’ve got the name but I need to get the wins amount. The wins amount is stored as an int value inside leaderstats folder. Here is my datastore script:

local Players = game:GetService('Players')
local DataStoreService = game:GetService('DataStoreService')

local WinsDataStore = DataStoreService:GetDataStore('Wins')


Players.PlayerAdded:Connect(function(Player)
	
	local Stats = Instance.new('Folder')
	Stats.Name = 'leaderstats'
	Stats.Parent = Player
	
	local Wins = Instance.new('IntValue')
	Wins.Name = 'Wins'
	Wins.Parent = Stats
	
	
	local Data = WinsDataStore:GetAsync(Player.UserId)
	
	
	if Data then
		for name, value in pairs(Data.Stats) do
			Stats[name].Value = value
		end
		
	end
	
		
		
end)

Players.PlayerRemoving:Connect(function(Player)
	local SaveData = {Stats = {}}
	

	
	for _, stat in pairs(Player.leaderstats:GetChildren()) do
		SaveData.Stats[stat.Name] = stat.Value
	end
	

	
	
	
	WinsDataStore:SetAsync(Player.UserId,SaveData)
	
	
end)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		local SaveData = {Stats = {}}
		

		
		for _,stat in pairs(Player.leaderstats:GetChildren()) do
			SaveData.Stats[stat.Name] = stat.Value
		end
		

		
		WinsDataStore:SetAsync(Player.UserId,SaveData)
		
	end
	
	wait(2)
end)

I tried doing this:

local DataStoreService = game:GetService('DataStoreService')

local WinsDataStore = DataStoreService:GetDataStore('Wins')

script.Parent.UserId:GetPropertyChangedSignal('Value'):Connect(function()
	local PlayerName = game.Players:GetNameFromUserIdAsync(script.Parent.UserId.Value)
	local Player = game.Players:GetPlayerByUserId(script.Parent.UserId.Value)
	script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.PlayerName.Text = PlayerName
	script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.WinsAmount.Text = Player.leaderstats.Wins.Value
end)

But I get the error:
[10:39:45.092 - Workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs.1.Script:5: attempt to index nil with ‘leaderstats’]

Because the player isn’t in the game. How can I get the players leaderstats even if they’re not in the game if I have their datastore?

2 Likes

I’ve heard this plugin, Datastore editor, might be able to help you out.

I think you would need to use an I,V pairs loop with the data you have stored, and make a variable that gets the highest number by setting the highest as 0 and then checking if the number is bigger than the highest, and if it is, make the “highest” variable the higher value.

I’m not good at datastores, can you tell me how?

Unfortunately, I don’t even work with data stores :confused: I find it hard to understand and have no motivation to try to learn them anymore.

You are going to have to ask someone other than me for this question.

I tried doing this:

local DataStoreService = game:GetService('DataStoreService')

local WinsDataStore = DataStoreService:GetDataStore('Wins')


script.Parent.UserId:GetPropertyChangedSignal('Value'):Connect(function()
	local PlayerName = game.Players:GetNameFromUserIdAsync(script.Parent.UserId.Value)
	local Player = game.Players:GetPlayerByUserId(script.Parent.UserId.Value)
	local Data = WinsDataStore:GetAsync(Player.UserId)
	
	if Data then

		script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.PlayerName.Text = PlayerName
		script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.WinsAmount.Text = Data.Stats
	end		
end)

and I’m getting the error: [11:31:47.478 - Workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs.1.Script:9: attempt to index nil with ‘UserId’]

Hello!!
Correct me if im wrong but I think in here:

You dont need to write UserId.Value, just UserId.
Try that😁