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