Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does leaderstats not show up in my player? (player.leaderstats)

I'm using Datastore 2 and sometimes, when I play, I get an error: "leaderstats is not a valid member of Player "Players.Hoogidy_Boogidy" - Client - DisplayCoinsAmount:4". If you used Datastore 2 before, or watched Alvin Blox's tutorial about it, then you know that you can save data in studio by adding a Boolean Value set to true in ServerStorage. If leaderstats isn't showing up because of that, then please tell me.

I have 2 scripts. The first one is "DisplayCoinsAmount", which is the one that gave the error and the one that displays the amount of coins the player has, in a GUI. The second script is "SaveData", which is the one that creates the Folder, "leaderstats" and saves the coins for the player. I can't get Roblox studio to give me the error again. It only happened twice.

Here is my 2 scripts:

DisplayCoinsAmount:

local plr = script.Parent.Parent.Parent.Parent.Parent

game:GetService('RunService').RenderStepped:Connect(function()
	script.Parent.Text = tostring(plr.leaderstats.Coins.Value)
end)

SaveData:

local DS2 = require(1936396537)
local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(plr)
	local coinsDS = DS2('Coins', plr)
	local leaderstats = Instance.new('Folder', plr)
	local coins = Instance.new('IntValue', leaderstats)
	
	leaderstats.Name = 'leaderstats'
	coins.Name = 'Coins'
	
	local function updateCoins(updatedValue)
		coins.Value = coinsDS:Get(updatedValue)
	end
	
	updateCoins(DefaultValue) -- Idk what these two lines are supposed to do
	coinsDS:OnUpdate(updateCoins) -- I just copied Alvin Blox :|
	
	while wait() do
		coins.Value = coinsDS:Get()
	end
end)

I would appreciate if you would help! Thank you! :D

Edit:

I tried using game.Loaded, but I don't know what that does

Code:

local DS2 = require(1936396537)
local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(plr)
	local coinsDS = DS2('Coins', plr)
	
	repeat wait() until(game.Loaded)
	
	local leaderstats = Instance.new('Folder', plr)
	local coins = Instance.new('NumberValue', leaderstats)
	
	leaderstats.Name = 'leaderstats'
	coins.Name = 'Coins'
	
	local function updateCoins(updatedValue)
		coins.Value = coinsDS:Get(updatedValue)
	end
	
	updateCoins(DefaultValue)
	coinsDS:OnUpdate(updateCoins)
	
	while wait() do
		coins.Value = coinsDS:Get()
	end
end)

11 comments

0

I would reocmmend using normal datastore; much simpler.

0

Normal datastore is long, and DS2 saves data better.

0

does it show leaderstats in the player in the explorer (when you click play)?

0

Yes, but when I get the error, that means it isn't there.

0

Maybe try using "WaitForChild("")" a few times to get the player..?

0

maybe add a WaitForChild on leaderstats, maybe your client script is executing too fast before the server can load the leaderstats

0

Where would I add the WaitForChild?

0

Replace line 4 with -- script.Parent.Text = tostring(plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value)

0

That only gets rid of the error. But leaderstats isn't there. So, it's just waiting for leaderstats forever. Sorry for not replying sooner!!!!!!!

0

What do you mean by leaderstats isn't there, is it not showing up on the top right corner of the game?

0

it isn't showing in my player

2 answers

Sorry for the late answer, but this should work!

Problem: It's pretty hard to explain but the reason it didn't work was because the playerevent was declared after the variables, when it should be on top:

(Also ensure that this is in a server script (normal script) I changed your last script!

game.Players.PlayerAdded:Connect(function(plr)
local DS2 = require(1936396537)
local DefaultValue = 0

	local coinsDS = DS2('Coins', plr)

	repeat wait() until(game.Loaded)

	local leaderstats = Instance.new('Folder', plr)
	local coins = Instance.new('NumberValue', leaderstats)

	leaderstats.Name = 'leaderstats'
	coins.Name = 'Coins'

	local function updateCoins(updatedValue)
		coins.Value = coinsDS:Get(updatedValue)
	end

	updateCoins(DefaultValue)
	coinsDS:OnUpdate(updateCoins)

	while wait() do
		coins.Value = coinsDS:Get()
	end
end)

2 comments

1

Thx for trying!

0

message me when you get the correct answer!

put wait(3) on line 1 it will wait 3 seconds so it will be loaded and wont make error bcs it will find it. this is script for saving folders. if someone need normal saving for folders :)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"
	
	local money = Instance.new("NumberValue", folder)
	money.Name = "Money"
	
	local playerUserId = "Player_"..player.UserId
	local data = playerData:GetAsync(playerUserId)
	if data then
		money.Value = data["Money"]
		-- here u can add more values from top
	else
		money.Value = 0
	end
end

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = "Player_"..player.UserId
		playerData:SetAsync(playerUserId, player_stats)
	end)
	if not success then
		warn("Could not save data")
	end
end

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

4 comments

0

I'm using Datastore 2

0

but wait should help

0

what if the player didn't fully load?

0

Sorry for not replying sooner

This discussion is preserved as it was. New answers, comments, and votes are closed.