Leaderstats somehow broken?

Hi! I’m Keanny.

After the latest Roblox update, I realized that my leaderstats (which worked perfectly fine before the update) was somehow broken. Every time I enter the game, I get this error:

leaderstats is not a valid member of Player

I checked the player file in Roblox Studio while the game was running and the leaderstats folder is missing. Here is the code that used to work, but doesn’t work now:

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
end

Does anyone know how to fix this? It would be amazing if someone can tell me what went wrong. Thanks in advance.

-Keanny

1 Like

i suppose you use this

 game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
end

It is recommended to parent the leaderstats to the player once every other stat has been added to it. This reduces the amount of calls Roblox has to do.

Here is some fixed code:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats

    leaderstats.Parent = plr
end)

It doesn’t look like that error is coming from this leaderstats script. Do you have another script that is accessing the players leaderstats that is causing this?

actually, I forgot to include the one line of code on the top, but on the original script it’s there. The error is coming from a gui where I display the amount of money a player have. Since there is no leaderstats, it returns that error. To confirm that the error wasn’t actually caused by that script, I attempted to change the amount of money I have on the developer’s console by inserting this code:

game.Players.keanwei01.leaderstats.Money.Value = 1000000000

…to which it returned:

leaderstats is not a valid member of Player

I’m certain that the leaderstats isn’t in the player and this script is the only one that handles the leaderstats.

You also forgot the bracket at end

Game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
end) -- keep the bracket
players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Value = 100000000
	Money.Parent = leaderstats
end

Try doing this, the line you sent errors because the leaderstats folder is not inserted into the player yet

What if leaderstats didn’t load yet?
maybe try

game.Players.keanwei01:WaitForChild("leaderstats").Money.Value = 1000000000

I got this:

Infinite yield possible on 'Players.keanwei01:WaitForChild("leaderstats")'

Pretty sure the leaderstats wasn’t loaded at all.

Well, that’s another typo in the script I posted. Original script has the bracket.

Oh, I see! What do you think is causing the leaderstats folder to not be inserted into the player?

Can you post the full script? What you posted should work besides the missing parentheses.

Ok.

local dataStoreService = game:GetService("DataStoreService")
local moneyDataStore = dataStoreService:GetDataStore("moneyDataStore")
local garageDataStore = dataStoreService:GetDataStore("garageDataStore")
local players = game:GetService("Players")
local playerData = {}

local templateData = {
	garage = {}
}

local function loadPlayerData(plr)
	local PD = templateData
	local success, failMessage = pcall(function()
		local tries = 0
		local playerSave = nil
		repeat
			tries = tries + 1
			playerSave = garageDataStore:GetAsync(plr.UserId.."-garage")
		until playerSave or tries == 3
		if playerSave then
			PD = playerSave
		end
	end)
	return PD
end

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Value = 2000
	Money.Parent = leaderstats
	
	local moneyData
	local success, failMessage = pcall(function()
		moneyData = moneyDataStore:GetAsync(plr.UserId.."-cash")
	end)
	if success then
		Money.Value = moneyData
		print(moneyData)
		print("tada!")
	else
		print("whoops!")
		warn(failMessage)
	end
	
	local loadedData = loadPlayerData(plr)
	local garage = loadedData.garage
	playerData[plr.Name] = loadedData
	
	for i = 1, #playerData[plr.Name].garage do
		--update car names here
		if playerData[plr.Name].garage[i] == "Toyota Avalon" then
			playerData[plr.Name].garage[i] = "Toyota Avalon 2019"
			print(playerData[plr.Name].garage[i])
		elseif playerData[plr.Name].garage[i] == "Koenigsegg CC8S" then
			playerData[plr.Name].garage[i] = "Koenigsegg CCX"
			print(playerData[plr.Name].garage[i])
		elseif playerData[plr.Name].garage[i] == "Toyota Yaris" then
			playerData[plr.Name].garage[i] = "Toyota Yaris 2008"
			print(playerData[plr.Name].garage[i])
		end
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local success, failMessage = pcall(function()
		moneyDataStore:SetAsync(plr.UserId.."-cash",plr.leaderstats.Money.Value)
	end)
	if success then
		print("Done saving.")
	else
		print("whoops!")
		warn(failMessage)
	end
	
	local success, failMessage = pcall(function()
		garageDataStore:UpdateAsync(plr.UserId.."-garage",function(oldValue)
			local newValue = oldValue or templateData
			local plrData = playerData[plr.Name]
			if not plrData then return end
			for i, data in pairs(plrData) do
				newValue[i] = data
			end
			return newValue
		end)
	end)
	if success then
		print("Done saving.")
	else
		print("whoops!")
		warn(failMessage)
	end
	
	local w = workspace:GetChildren()
	for i = 1, #w do
		if string.find(w[i].Name, plr.Name .. "'s ") then
			w[i]:Destroy()
		end
	end
end)

It should work, nothing is wrong with the leaderstats. I tested the script in my own game and the leaderstats show up fine.

Maybe another script is interfering?

Well, I found the problem. It’s from a RemoteEvent declaration in the leaderstats script that I thought wouldn’t be the cause of the problem. Thanks for helping out though, and sorry for wasting your time.

2 Likes