Using table.find() to find an object using its name

I need to know if it is actually possible to find an element on a list using only its name.

example:

local Model = Instance.new(“Model”)

Model.Name = “Model1”

local list = {Model}

local find = table.find(list, “Model1”)

An Instance as far as im aware has a specific id like for example: Instance(12345) when inside a table, you can probably do this:

tab[model.Name] = model -- Assigns index using Instance Name

print(tab[model.Name]) -- Instance or nil

let me try it out (wfadgsdgfdgfgdg)

Prints nil (wordssssssssssssss)

To answer your question no, because multiple instances can have the same name. What Roblox compares as @DasKairo mentioned is the instance itself. So to find Model(the instance not the string) you have to search for Model in the table instead of its name. However, if you know the table has only instances and of different names and that it’s not a huge amount of them(if the amount is big you should use a custom search algorithm or way of storing data) you can also try the silly solution:

function getIndexOfInstance(t: {Instance}, name: string): number?
	for i, v in pairs(t) do
		if v.Name == name then return i end 
	end
	return nil
end
1 Like

That particular snippet code would work. Try this:

local Model = Instance.new(“Model”)

Model.Name = “Model1”

local list = {
  [Model.Name] = Model
}

local model = list[Model.Name]
print(model) --> "Model1"
1 Like

Im trying to find the instance, Imma try using foreach cause I can’t use for loops if I don’t want to crash the game, cause going trough 600 chunks is bad

foreach is Deprecated.

1 Like

Foreach and for loops are the exact same except based on syntax. If you’re having to do massive searches, you should structure your table differently (for example like the above).

1 Like

the problem here is that they are continously been moved as they’re loaded.
I’ve managed to solve one part of this problem using attributes on every chunk, however when they start loading and unloading they get all moved and their position isn’t the same

As @unix_system mentioned if you plan to do massive searches you will have to figure out a smart way to quickly find data which means the solution is a custom way of storing it(for example as a dictionary where the key is the name) or a custom way of searching it(for example a custom implementation of binary search that satisfies your way of storing data). However, I assume for your use case storing data in an easily accessible way is the way to go(under of course the assumption that all instances have different names).

1 Like

data is stored using dictionaries, the problem is that I need to find data without their position on the dictionary and only having their name, as they all have different names

--where chunk-1 is the name of the chunk Instance and chunks is the dictionary with all of them
--they must have been stored in the form of chunks[instance.Name] = instance
local data = chunks["chunk-1"]
if data then
	print(data)
elseif data == nil then
	print("chunk-1 doesn't exist")
end
1 Like

Try this

local Parent = script.Parent --Change this to the instance you want to find the object inside
local ToFind = "NameOfModel" --Change to the models name

function FindByName(GivenName: String, GivenParent: Instance)
    local names = {}
    for i,v in pairs(GivenParent:GetChildren()) do
        table.insert(names,v.Name)
    end
    if table.find(names,GivenName) then
        return GivenParent[GivenName]
    else
        return nil
    end
end

--[[      TEST
tell me what came out in the output!      ]]
print(tostring(FindByName(ToFind,Parent)))

I can’t use for loops as they crash the game as I have about 650 chunks inside a list and every chunk contains 100 instances aprox.

Then you cant use table.find() to search for something
This would work (tested it) but you need to find an other solution then

1 Like

Imma try to come out with something that works instead of table.find().

TY

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.