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?