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

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

Asked by 5 years ago
Edited 5 years ago
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)

0
We’d need the full Script to analyze your issue properly Ziffixture 6913 — 5y
0
ok Narrowlegobricks 12 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
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 Narrowlegobricks 12 — 5y
0
No problem, once the "PlayerStats" is a child of the player, the yield will stop SerpentineKing 3885 — 5y
Ad

Answer this question