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.
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:
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)
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.