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

Im getting an error saying that PlayerStats is not a valid member of player. How do I fix this?

local Player = game.Players.LocalPlayer
 
local BagText = script.Parent.BagLabel
 
local CoinText = script.Parent.CoinLabel
 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
 
BagText.Text = "Bag: " .. Player.PlayerStats.CurrentValue.Value .. "/" .. Player.PlayerStats.Storage.Value
CoinText.Text = "Coins: " .. Player.PlayerStats.Coins.Value
 
Player.PlayerStats.CurrentValue:GetPropertyChangedSignal("Value"):Connect(function()
    BagText.Text = "Bag: " .. Player.PlayerStats.CurrentValue.Value .. "/" .. Player.PlayerStats.Storage.Value
end)
 
 
Player.PlayerStats.Coins:GetPropertyChangedSignal("Value"):Connect(function()
    CoinText.Text = "Coins: " .. Player.PlayerStats.Coins.Value
end)

2 comments

0

We’d need the full Script to analyze your issue properly

0

ok

1 answer

✓ Accepted answer

Your LocalScript is most likely running too early for the PlayerStats to be created, and as a result, it is not parented under the player yet.

By adding a local variable pertaining to the "PlayerStats" and using "WaitForChild()" you can make sure that the functions are only able to be run when "PlayerStats" is a member of the player.

local Player = game.Players.LocalPlayer
local Stats = Player:WaitForChild("PlayerStats")
local BagText = script.Parent.BagLabel
local CoinText = script.Parent.CoinLabel

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents

BagText.Text = ("Bag: "..Stats.CurrentValue.Value.."/"..Stats.Storage.Value)
CoinText.Text = ("Coins: "..Stats.Coins.Value)

Stats.CurrentValue:GetPropertyChangedSignal("Value"):Connect(function()
    BagText.Text = ("Bag: "..Stats.CurrentValue.Value.."/"..Stats.Storage.Value)
end)


Stats.Coins:GetPropertyChangedSignal("Value"):Connect(function()
    CoinText.Text = ("Coins: "..Stats.Coins.Value)
end)

In order to be extra sure, you can do the same for the values inside the function

local Player = game.Players.LocalPlayer
local Stats = Player:WaitForChild("PlayerStats")
local Current = Stats:WaitForChild("CurrentValue")
local Storage = Stats:WaitForChild("Storage")
local Coins = Stats:WaitForChild("Coins")

local BagText = script.Parent.BagLabel
local CoinText = script.Parent.CoinLabel

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents

BagText.Text = ("Bag: "..Current.Value.."/"..Storage.Value)
CoinText.Text = ("Coins: "..Coins.Value)

Current:GetPropertyChangedSignal("Value"):Connect(function()
    BagText.Text = ("Bag: "..Current.Value.."/"..Storage.Value)
end)


Coins:GetPropertyChangedSignal("Value"):Connect(function()
    CoinText.Text = ("Coins: "..Coins.Value)
end)

2 comments

0

Thank you very much serpentine king. Now all i receive is an infinite yield for :WaitForChild. Again thanks a lot for looking into my problem

0

No problem, once the "PlayerStats" is a child of the player, the yield will stop

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