Table.find huge issue

im attempting to find a part called “wall” in a table however each time i use table.find it only returns a nil value

i have tried every single solution and asked everyone i knew on the problem but they had no clue

please help

p.s im not good on explaining so sorry if i didn’t give enough information

Giving a script would be helpful

I can only guess you are trying to find an item using a string instead of an instance

1 Like

-- Function to find issues in a large table
local function findIssuesInTable(t)
    local issues = {
        nilValues = {},
        duplicates = {},
        specificValueConditions = {},
    }
    
    -- Check for nil values and duplicates
    local seen = {}
    for i, v in ipairs(t) do
        if v == nil then
            table.insert(issues.nilValues, i)
        end
        if seen[v] then
            table.insert(issues.duplicates, {value = v, indices = {seen[v], i}})
        else
            seen[v] = i
        end
    end

    -- Example of specific value condition: check for negative numbers
    for i, v in ipairs(t) do
        if type(v) == "number" and v < 0 then
            table.insert(issues.specificValueConditions, {index = i, value = v})
        end
    end

    return issues
end

-- Example usage
local exampleTable = {1, 2, nil, 4, 2, -5, 6, nil, -3}
local issues = findIssuesInTable(exampleTable)

print("Nil values found at indices:")
for _, index in ipairs(issues.nilValues) do
    print(index)
end

print("Duplicates found:")
for _, dup in ipairs(issues.duplicates) do
    print("Value: " .. dup.value .. ", Indices: " .. table.concat(dup.indices, ", "))
end

print("Specific value conditions (negative numbers) found:")
for _, cond in ipairs(issues.specificValueConditions) do
    print("Index: " .. cond.index .. ", Value: " .. cond.value)
end

Did you put the instance in the table or it’s name? You’d need to search with whatever value you put in.

i put in an instance into the table

yeah thats what im doing because i dont know how to get the instance from the table

also heres the script

image

You can’t search the instance using the string name associated with it. So you have to convert "wall" to the associated instance and look for that. Another solution is to check all the values:

local function find(t: {Instance}, name: string): Instance?
	for _, v in pairs(t) do
		if v.Name == name then return v end
	end
	return nil
end

print(find(frontitems, "wall"))

i know this is gonna sound a bit stupid but what do the :'s mean :sweat_smile:

Don’t know what’s the smart word for it, but basically you are marking that this specific variable is a string/boolean/instance/whatever datatype, which just helps you in the future understand what the heck are all these words. Code editor also automatically warns you if you are parsing an incorrect datatype and shows additional hints. It’s like comments on steroid

what about the underscore in the for loop what does it mean

im not exactly sure i quite understand could you dumb it down just a bit more

These “:” are called type annotations and doesn’t affect the way the code runs
“_” is kind of a “throwaway variable”, which basically means that you are not planning to use this variable in any way. Also has no effect

The code below is the same and you probably understand it more. All these fancy stuff is usually used by more expirienced scripters for easier communication between them

local function find(t, name)
	for i, v in pairs(t) do
		if v.Name == name then return v end
	end
	return nil
end

print(find(frontitems, "wall"))

thank you for explaining everything with details and being so patient im still very new to coding and i’ve been stuck with this problem for so long so im really happy to have this mysterious problem explained

so again thank you for explaining the details and for your patience, have a nice night

thank you aswell for giving me the the solution i needed and went out of your way to help me, im very thankful for your help and wish you alot of luck in what you are doing

thanks again, have a nice night

No problem. Everyone has to start from somewhere. All these stuff just comes in naturally (at least for me). If you really want to, you can pm me in the future for help

again, thank you for explaining the problem to me and for your time and patience, you do not know how thankful i am for someone finally having an answer to this problem i had. anyways, goodnight and thank you again

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