Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
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
Do not do Instance.new("NumberValue", plr). Define the Lvl's parent at the end (Lvl.Parent = plr). waifuSZN 123 — 7y

2 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago

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.

Ad
Log in to vote
0
Answered by 7 years ago

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

Answer this question