Find Instance by string

Is it possible to find an instance by it’s name or value (if it’s a stringvalue) based on searches using strings?

2 Likes

You can use the tostring() function I believe

Example:

local Target = workspace.Baseplate

local TargetString = tostring(ThingToString)
print(TargetString)
--Hopeful Output: "Baseplate" 

Or if you’re wanting to do it using a StringValue you can do this I believe:

local String = workspace.Baseplate.StringValue
String.Value = "This is a test"

print(String.Value)
--Output: "This is a test"
2 Likes

To search by an instance by name you would use, for example: script.Parent:FindFirstChild(instanceName) or script.Parent[instanceName]

5 Likes

What if I used GetChildren to get a bunch of instances but I only want to use one, is it possible to get the one desired instance by searching for it using strings?

Example

Lets say there is a folder in workspace named folder with 5 parts

local Intstances = game.Workspace.Folder:GetChildren()

Is this possible?
local DesiredItem = table.find(Instances,“Part3”) - Looking to get part 3 out of all 5 parts

or
local DesiredItem = Instance[“Part3”] - Same request

Edit: Fixed format

2 Likes

That wont work I believe, the 2nd one I know wont work since you have to give it an index, not a string. And for the 1st one, it wouldn’t give anything since the children are instances, not strings

2 Likes

Do you mean if all the children are named part and you want to index one? If so you would do:

local Instances = game.Workspace.Folder:GetChildren()

local part3 = Instances[3]
1 Like

You can find the instance with their name using

Instance:FindFirstChild(Name)

And for values you’ll have to index all of the instances and check their Value one by one.

2 Likes

The former would work in this case (with a slight adjustment), as calling :GetChildren() on an Instance will return an array, which has an index/value pair. On the other hand, the latter would be used for dictionaries that have a key value pair.

local instancesArray = {"Part1", "Part2", "Part3", "Part4", "Part5") -- This is not how it'd be stored normally when you call :GetChildren() since these would be the Name property of the Instance

local instancesDictionary = {

    ["Part1"] = "ValueHere",
    ["Part2"] = "ValueHere",
    ["Part3"] = "ValueHere",
    ["Part4"] = "ValueHere",
    ["Part5"] = "ValueHere",

}

table.find(instancesArray, "Part3") -- This would work for the Array
instancesDictionary["Part3"] -- This would work for the Dictionary

Since the array that is created when calling :GetChildren() on the Instance will contain the actual Instances, you’d need to look for the Instance itself unless you stored the name.

A loop could be used for Instances in this case, too:

local newInstancesArray = workspace.partFolder:GetChildren()

for _, item in ipairs(newInstancesArray) do
    if item.Name == "Part3" then
       -- Continue
    end
end

More info about tables can be found on this Developer Hub Article.

3 Likes

Thanks for indirectly correcting me that one!

I just edited the post a bit because I made a slight oopsie – while table.find(instancesArray, "Part3") could be used to find it in an array with strings, :GetChildren() on an Instance will return the Instances themselves which means that using table.find for it would only work if there was a reference to the actual part somewhere else:

CC @Darobbap for more info

local partFolder = workspace.partFolder
local array = partFolder:GetChildren()

local Part3 = partFolder.Part3

if table.find(array, Part3) then
    -- Continue
end
6 Likes

Ohhh! Now I understand clearly! Thank you again for helping me and others understand!

2 Likes