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 6 years ago
Edited 6 years ago
01local Player = game.Players.LocalPlayer
02 
03local BagText = script.Parent.BagLabel
04 
05local CoinText = script.Parent.CoinLabel
06 
07local ReplicatedStorage = game:GetService("ReplicatedStorage")
08local RemoteEvents = ReplicatedStorage.RemoteEvents
09 
10BagText.Text = "Bag: " .. Player.PlayerStats.CurrentValue.Value .. "/" .. Player.PlayerStats.Storage.Value
11CoinText.Text = "Coins: " .. Player.PlayerStats.Coins.Value
12 
13Player.PlayerStats.CurrentValue:GetPropertyChangedSignal("Value"):Connect(function()
14    BagText.Text = "Bag: " .. Player.PlayerStats.CurrentValue.Value .. "/" .. Player.PlayerStats.Storage.Value
15end)
16 
17 
18Player.PlayerStats.Coins:GetPropertyChangedSignal("Value"):Connect(function()
19    CoinText.Text = "Coins: " .. Player.PlayerStats.Coins.Value
20end)
0
We’d need the full Script to analyze your issue properly Ziffixture 6913 — 6y
0
ok Narrowlegobricks 12 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 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.

01local Player = game.Players.LocalPlayer
02local Stats = Player:WaitForChild("PlayerStats")
03local BagText = script.Parent.BagLabel
04local CoinText = script.Parent.CoinLabel
05 
06local ReplicatedStorage = game:GetService("ReplicatedStorage")
07local RemoteEvents = ReplicatedStorage.RemoteEvents
08 
09BagText.Text = ("Bag: "..Stats.CurrentValue.Value.."/"..Stats.Storage.Value)
10CoinText.Text = ("Coins: "..Stats.Coins.Value)
11 
12Stats.CurrentValue:GetPropertyChangedSignal("Value"):Connect(function()
13    BagText.Text = ("Bag: "..Stats.CurrentValue.Value.."/"..Stats.Storage.Value)
14end)
15 
16 
17Stats.Coins:GetPropertyChangedSignal("Value"):Connect(function()
18    CoinText.Text = ("Coins: "..Stats.Coins.Value)
19end)

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

01local Player = game.Players.LocalPlayer
02local Stats = Player:WaitForChild("PlayerStats")
03local Current = Stats:WaitForChild("CurrentValue")
04local Storage = Stats:WaitForChild("Storage")
05local Coins = Stats:WaitForChild("Coins")
06 
07local BagText = script.Parent.BagLabel
08local CoinText = script.Parent.CoinLabel
09 
10local ReplicatedStorage = game:GetService("ReplicatedStorage")
11local RemoteEvents = ReplicatedStorage.RemoteEvents
12 
13BagText.Text = ("Bag: "..Current.Value.."/"..Storage.Value)
14CoinText.Text = ("Coins: "..Coins.Value)
15 
View all 23 lines...
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 — 6y
0
No problem, once the "PlayerStats" is a child of the player, the yield will stop SerpentineKing 3885 — 6y
Ad

Answer this question