Error in Script that I cant fix

I need help with this script. I’m getting an error that says "leaderstats is not a valid member of Player “Players.hesBlame”

I’m trying to make Datastores for the leaderstats.

Script:

local DSS = game:GetService("DataStoreService")
local WillPowerDS = DSS:GetDataStore("WillPowerData")
local MoneyDS = DSS:GetDataStore("MoneyData")
game.Players.PlayerAdded:Connect(function(plr)
	
	local MoneyData 
	local Completed, ErrorMessage =	pcall(function()
		MoneyData = MoneyDS:GetAsync("Money-" .. plr.UserId, plr.leaderstats.Money.Value)
		
		
	end)
	if Completed then
		print("Money for " .. plr.Name .. " has loaded.")
	else
		print(ErrorMessage)
	end
	
	local WillPowerData
	local success, Errors = pcall(function()
		WillPowerData = WillPowerDS:GetAsync("WillPower-" .. plr.UserId, plr.leaderstats.WillPower.Value)
		
	end)
	if success then
		print("WillPower for " .. plr.Name .. " has loaded.")
	else
		print(Errors)
	end
	
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = plr

	local stage = Instance.new("IntValue")
	stage.Name = "WillPower"
	stage.Value = WillPowerData or "0"
	stage.Parent = ls

	local currency = Instance.new("IntValue")
	currency.Name = "Money"
	currency.Value = MoneyData or "0"
	currency.Parent = ls
	
end)
2 Likes

You cant reference leaderstats until you create it later in the script
Either create and parent leaderstats before referencing it or move the pcalls below where you create it

If I move the pcalls below, how will the leaderstats load from its save?

GetAsync only has 1 argument describing the datastore key
I didnt notice before, so thats likely your problem
The references to leaderstats arent necessary and whatever youre trying to do should be implemented another way

An example of properly loading a value from a datastore into a leaderstats folder would be something like:

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr

local money = Instance.new("NumberValue")
money.Name = "Money"
money.Parent = leaderstats

local moneyData
local success = pcall(function()
    moneyData = ds:GetAsync(plr.UserId)
end)

if success then
    money.Value = moneyData
end
1 Like

Thing is, I don’t understand why this doesn’t work and my other script does. I made a script where the stages save and it CFrames you to the current stage and also saves your money, and it looks EXACTLY the same like the current script here, just with loading the character and cframing when dead or loading in.
(i could show u the script too)

Ah i see you only need the key parameter in the getasync, remove the other things.

2 Likes

It looks like you are putting the parameters for :SetAsync in GetAsync. Just put the key name in.

The reason you are getting error is because you are calling the leaderstats (as the second parameter of :getasync which you should take out) before you made the leaderstats folder

1 Like