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

Money is not a valid member of Folder "Players.Verbo_Pro.leaderstats", what to do?

Asked by 2 years ago

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!

2 answers

Log in to vote
1
Answered by
Antelear 185
2 years ago
Edited 2 years ago

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!

0
Very well explained! I'm impressed. NotThatFamouss 605 — 2y
0
Thank you very much! You're the best!!! Who would have thought that the problem was in this gap, and I didn't even notice!!! Thanks!!!! Verbo_Pro 2 — 2y
0
Heh, you're welcome. Antelear 185 — 2y
Ad
Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

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:

DevHub: Instance:GetPropertyChangedSignal

DevHub: Instance:WaitForChild

DevForum: GetPropertyChangedSignal/Changed by ReturnedTrue

0
It's also because he did "Money " with the whitespace. The script won't recognise the directory without him using `["Money "]`. Antelear 185 — 2y
0
Also you don't need 2 variables for LocalPlayer in this case, we JUST need the LocalPlayer. So all you needed to do would've been: "local User = game:GetService("Players").LocalPlayer". But good explanation! Upvote Antelear 185 — 2y
0
Just my style of writing scripts :) It looks more read-able to me, and thank you! NotThatFamouss 605 — 2y
0
Thanks, I will. Verbo_Pro 2 — 2y
View all comments (14 more)
0
May I ask? Why did you use GetPropertyChangedSignal? For optimization? That is, it is better not to use the while loop in this case? Verbo_Pro 2 — 2y
0
While loops are inefficient and is slow, while GetPropertyChangedSignal isn't and you can even disconnect it to free up some spaces. (From the DevForum post) Although according to the DevForum post, Changed are better to use for ValueBases, I personally use GetPropertyChangedSignal more. For more information, please read the DevForum post I provided in the Sources. NotThatFamouss 605 — 2y
0
Thanks, I read it. But because I do not know English and use a translator, I did not catch the essence. Taking a moment to ask, is it OK to use for storing data (such as money, strength, strength, speed, lvl) leaderstats? And let's say I want to remove Money from the leaderboard so that they would remain, but not be shown. Sorry for the rudeness, this is my last question and I'll leave you alone. Verbo_Pro 2 — 2y
0
If you mean storing value but not displaying it in the game, with proper practices yes, it's ok to store values in a data. But I think you would still have to store it in the player instance for the Client to be able to access it. And do you speak Indonesian? I can speak Indonesian and I can probably translate things for you. And also don't apologize for being curious! You can always ask question NotThatFamouss 605 — 2y
0
No, I don't speak Indian) Can you please give me a resource where I can see where to store data for the player. And I also asked how to make it so that the leaderboard does not display (speed, money, strength)? I store data in leaderstats and probably it's wrong. Thank you for your help, I recently got inspired and want to do something like Anime Fighting Simulator. Verbo_Pro 2 — 2y
0
As for the leaderstats data, I think Roblox will only display it as a leaderstats if the name of the Folder is "leaderstats" so to make it so that it doesn't display, you can probably use other names. And again, it's absolutely OK to store data with proper practices. NotThatFamouss 605 — 2y
0
Thank you very much! You have helped me a lot! Do you have your own modes in roblox? Verbo_Pro 2 — 2y
0
By modes did you mean games? NotThatFamouss 605 — 2y
0
Yes Verbo_Pro 2 — 2y
0
I am in the process of making my own game. Though I'm feeling unmotivated to work on it. NotThatFamouss 605 — 2y
0
Cool! I wish you good luck! Can I see it? Verbo_Pro 2 — 2y
0
Thanks! And I can't really let anyone see it since I just started it a few days ago. I'm trying to keep myself on track and finish the game though. NotThatFamouss 605 — 2y
0
Well Verbo_Pro 2 — 2y

Answer this question