How to find the part instead of name

I am trying to get the part of the tool instead of the name with this code/
How can i change “Handle” the name to find the part instead? while keeping the available tools.

game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"

2 Likes

Just change the first line to a variable, and then change it to

[Variable].Handle

Since all handles need to be named “Handle” to my knowlege

2 Likes

What exactly do you mean when you say find the part instead of the name? Aren’t you already doing that?

FindFirstChild returns the first object to have the same name as what you pass in, or nil if none of the children (descendants if recursive is true) have the name specified.

1 Like

should be:

game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1]).Handle

When using "..", this combines (or concatenates) two strings into one string, example:

print("He" .. "llo") --> Hello

Also, side pointer, if the weapon doesn’t exist for some reason this code will error. If you expect it to always exist, use WaitForChild instead of FindFirstChild.
Or - if it sometimes may not be there, you should do the following:

local Weapon = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1])

if Weapon then
    -- code here
    Weapon.Handle.BrickColor = BrickColor.Random() -- example
else
    print("Weapon didn't exist!")
end

If you need anything explained further let me know and I’ll do my best to help you out!

2 Likes

No i am trying to find the Part like i can use FindFirstChild of Part instead of handle which is the name

I’m sorry, I don’t think I understand what you’re trying to achieve :L

I would suggest searching up some Roblox Lua tutorials as you do not seem to understand the basics of objects/variables.

When you use FindFirstChild or reference a part by using a Variable that variable becomes a direct reference to the part meaning you can then use that variable to edit the part.

Example:

local Part = workspace:FindFirstChild("Ball")
Part.BrickColor = BrickColor.red()

Though this is a simple example it means the “Part” variable is now a direct reference to the “Ball” object in workspace.

1 Like

I know i make no sense but what i want is to find the part of all the items in my folder instead of its name.

This code finds the folder called toolmodels and availbetools is getting all the tools, the “Handle” gets all the tools that has the name Handle in them ex(“GunHandle”, ShotgunHandle"), i want to get the tools from their ClassName like Part or MeshPart. But Changing Handle to Part does not work because Part is not a name.

game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle")

If you want to do something with every part inside of the folder, do

for _, child in pairs( game.ReplicatedStorage:WaitForChild("ToolModels") ) do
    if child:IsA( 'Part' ) then
        print( child )
    end
end

If you are after just one part, and you know it is the only part in the model/folder then you can use

game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChildWhichIsA( 'Part' )
2 Likes

It works but i need theavaiableTools[i][1] to be in there because if i do this, even tho the statement is true it will change every item so i need the avaibletools to make it sepearte for each tools. Like i want one item to be bought with cash and another with coins.

tihs does not work

game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChildWhichIsA(availableTools[i][1]..'Part')

Provide a picture of the hierarchy in studio and a demo of what availableTools looks like/contains.

I suspect a combination of misunderstanding and a subtle XY problem is stopping us from getting to the solution

:FindFirstChildWhichIsA() only works with ClassName.

1 Like

Is this what you mean?

game.ReplicatedStorage:WaitForChild(“ToolModels”):FindFirstChildOfClass(‘BasePart’).Handle

The thread is very confusing to what you’re attempting to achieve.

2 Likes