Hello, I'm trying to make the intValue of leaderstats. Money appear in the player's GUI. However, it gives an error: Money is not a valid member of Folder "Players.Verbo_Pro.leaderstats".
The script is located in the TextLabel
local moneyText = script.Parent while true do wait(1) local stats = game.Players.LocalPlayer.leaderstats print(stats.Money) moneyText.Text = "Money: " end
The script is located in ServerScriptService
local function onPlayerAdd(player) local stats = Instance.new("Folder") stats.Name = "leaderstats" stats.Parent = player local money = Instance.new("IntValue") money.Name = "Money " money.Value = 0 money.Parent = stats end game.Players.PlayerAdded:Connect(onPlayerAdd)
If I change line 7 in the first script to:
print(stats)
Then output gives the name and no errors. I hope for your help. Thank you in advance!
I'm pretty sure you shouldn't have that space on this line:
money.Name = "Money "
The reason is that if you put a space there and try to get the directory of it via game.Players.LocalPlayer.leaderstats.Money
, since the space isn't there it won't recognise the directory and error. Because in this case:
Yes. Players.Verbo_Pro.leaderstats.Money
does NOT exist. Just remove the space from the line I shown above and you shouldn't have much problems!
(Also, please make sure to use :WaitForChild()
when trying to get a value that probably has NOT loaded yet. It is important you do so to make sure the script doesn't error since it didn't find the stat that hasn't loaded yet. Example:)
local Stat = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Money")
This is to make sure the game has waited for the child to load before anything. However make sure to look out for "infinite yields", it means whenever you :WaitForChild() for something that doesn't exist yet in a certain place, or anywhere at all, it will say "infinite yield possible on . . .".
Just thought I'd let ya know!
I suspect the script doesn't have enough time window to load in the instance. A fix to this would probably be using Instance:WaitForChild()
I'll also provide a (I think) better practice of doing it.
local Players = game:GetService("Players") local plr = Players.LocalPlayer local Indicator = script.Parent local leaderstats = plr:WaitForChild("leaderstats") -- // We wait until the leaderstats were inserted/loaded by the script. local Money = leaderstats:WaitForChild("Money") Indicator.Text = Money.Value Money:GetPropertyChangedSignal("Value"):Connect(function() Indicator.Text = Money.Value end)
Sources: