Referencing certain things

Hi.
In my game, I have a folder called leaderstats, AND a string value called leaderstats.
However, my game cannot choose between the two, causing all sorts of errors.
Is there a way to tell the script to look in the folder only?

Thanks.

1 Like

You can use :FindFirstChild to directly index objects and if you want to find objects of a specific class then use :FindFirstChildOfClass.

An implementation of this would be

local leaderstats = player:FindFirstChild("Leaderstats")
local strLeader = leaderstats:FindFirstChild("leaderstats")
1 Like

You can use Player:FindFirstChildOfClass(“Folder”) to get the folder only. This will return the first folder it finds. You should find the folder and compare the name. I wouldn’t suggest having two children with the same in parented to the same instance,

1 Like

May I ask why you are doing this? I think the issue is design.

I would just change one of the two objects names. This is a flaw in your setup, consider making the names different.

2 Likes

Is the string value inside of the folder?

1 Like

I have tried that, but this is what Roblox says:

I have tried changing the name of both of them, but nothing seems to work.

So why do you have a StringValue called “leaderstats”? The values in the folder should have names like “Cash” or “Level” based on what they store.

1 Like

Renaming it would be easier, but if you really want to reference it then do

for _,v in pairs(parent:GetChildren()) do
if v.Name == "something" and v.ClassName == "somethingelse" then --you found it 
end
end

Also in your particular case you only have to name the folder “leaderstats”. By naming things the same you are digging yourself a grave.

1 Like

Here is what the leaderstats script inside my game look like:

 game.Players.PlayerAdded:Connect(function(player)
    	local leaderstats = Instance.new("Folder", player)
    	leaderstats.Name = "leaderstats"
    	
    	local tokens = Instance.new("IntValue", leaderstats)
    	tokens.Name = "Tokens"
end)

instead of using an IntValue use a StringValue

1 Like

I think your solution should be to change them because as I said, having two children with the same name can be detrimental to your workflow .

2 Likes

Thanks for the help.
I changed it :slight_smile:

image

Thank you for the help!
I changed it.

image

Make sure to mark one of our replies as the solution!