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

leaderstats is not a member of player?

Asked by 3 years ago

I am trying to make an inventory system in my game and I am trying to make a thing in a local script inside starter player scripts where it checks if a value has changed and then fires an event to see if they have reached max capacity, and I know this should work because I have done nearly the exact same thing with experience and levels but for some reason when I run the code it gives an error saying "leaderstats is not a member of player" does anyone know whats happening? Here is the code in the local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:FindFirstChild("InventoryFullEvent")

local player = game.Players.LocalPlayer
local playerBeans = player.leaderstats.Beans

playerBeans.Changed:Connect(function()
    Event:FireServer()
end)

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Hi goose, I believe I have solved your problem.

Leaderstats are made by a script on the server side, so they are not there to begin with. This means that, if you check in the 1st nanosecond of the server's existence, you won't find them at all.

To solve this problem, you should use the :WaitForChild() function, which will halt your script until the leaderstats appear. The same for the beans instance.

This is the code which I made and tested, all working for me at least:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:FindFirstChild("InventoryFullEvent")

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local playerBeans = leaderstats:WaitForChild("Beans")

playerBeans.Changed:Connect(function()
    Event:FireServer()
end)

Any problems, please do reply!

Hope this was helpful.

1
It works, thank you very much. Looking at my other script with levels and experience I apparently used WaitForChild and didn't notice. Thanks for the answer! GooseStranger 27 — 3y
Ad

Answer this question