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

"Lvl is not a valid member of Player" Help?

local module = {}
local Storage = game:GetService('DataStoreService'):GetDataStore("MoneyHolder")


game.Players.PlayerAdded:connect(function(plr)
    local Lvl = Instance.new("NumberValue", plr)
	Lvl.Name = "Lvl"
	Lvl.Value = 1
	
	local SavedStats = Storage:GetAsync(plr.userId .. "-Lvl")
	if SavedStats ~= nil then
		Lvl.Value = SavedStats
	end
end)


game.Players.PlayerAdded:connect(function(plr)
	local id = plr.userId
    local StatsValue = plr.Lvl.Value -- This line is the error, the error says "Lvl is not a valid member of Player"..I double checked, and Lvl is a valid member. Helpp
	Storage:SetAsync(id.. "-Lvl", StatsValue)
end)


return module

1 comment

1

Do not do Instance.new("NumberValue", plr). Define the Lvl's parent at the end (Lvl.Parent = plr).

2 answers

You're recieving this error because both PlayerAdded events fire simultaneously(for the most part). So, in the second event, plr.Lvl isn't going to exist yet.

You can get passed this by using WaitForChild in your second event, but a more efficient method to solving this would be simply combining your two events.

local Storage = game:GetService('DataStoreService'):GetDataStore("MoneyHolder")


game.Players.PlayerAdded:connect(function(plr)
	local Lvl = Instance.new("NumberValue", plr)
	Lvl.Name = "Lvl"
	Lvl.Value = 1
	local SavedStats = Storage:GetAsync(plr.userId .. "-Lvl")
	if SavedStats then
		Lvl.Value = SavedStats
	end
	Storage:SetAsync(plr.UserId.."-Lvl",Lvl.Value)
end)

I'm not sure why you were using a Module for this, and secondly i'm not sure why you're saving the data when they join in the first place.

The data is obviously being loaded potentially, so the only thing that would be saving is what the DataStores already have logged..

You could go without saving upon PlayerAdded completely, and just save using a PlayerRemoving event.

There could be two different possible explanations for this. One thing is that sometimes DataStore will randomly generate that error, but the code will still run as per usual. If it doesn't, try using this:

local StatsValue = plr:WaitForChild('Lvl').Value

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