Get the position in the table

Hello, i have this script bellow that is working.

local CanBeSeen = {["Nothing"] = game:GetService("Workspace").Scripting.CamNothing
}

local function findArray(name: string | number | boolean)
	local found = false
	local indexInt = 0

	for index, value in CanBeSeen do
		if found == false then
			indexInt += 1

			if index == name then
				found = true
			end
		end
	end

	if found == true then
		return indexInt

	else
		return nil

	end
end

print(findArray("Nothing"))

But is there a way to insert using table.insert method this in a table for exemple ?
["Base"] = game:GetService("Workspace").Baseplate

Or change the table for the FindArray can detect that :
local CanBeSeen = {{["Nothing"] = game:GetService("Workspace").Scripting.CamNothing}}

Sincerely,
@hollaquetalBRUH

3 Likes

you could use any type in this case instead of typing out every possible data type

table.insert is equivalent to the code below

local function insert(Table, Value)
    local i = #Table + 1
    Table[i] = Value
    return i
end

which means you cannot choose index value manually

in order to insert a value to the table at specific index you should use square parenthesis
for example

local SpecificIndex = "Base"
Table[SpecificIndex] = workspace.Baseplate

and if you want to change the value at specific index you do the same from above

also im not sure if you know this but table.find returns exactly what your function does

Then how i can table.insert this bellow ?
["Base"] = game:GetService("Workspace").Baseplate

Also if i do this table.find(“Nothing”) it return nil

i have provided an example in my post above on how to insert and change the value in a table at given index

yes thats because you need to tell this function in which table to search. the first argument would be the table to search in and the second argument would be the value to search for

also actually my bad table.find returns index of wanted value if there is any while your function searches for matching key in the table and returns how many keys it has gone through before finding desired one if there is one

could you please explain what are you trying to achieve because i cant see any use of this function above

i have opted for a better solution to do a table in a table like that :
local CanBeSeens = {
{[“Nothing”] = game:GetService(“Workspace”).Scripting.CamNothing}
}

local function findArray(name)
	local found = false
	local indexInt = 0

	for array,tab in ipairs(CanBeSeen) do
		indexInt +=1
		print(array, tab)
		if tab[name] then
			found = true
			return indexInt
		end
		if found == false then
			return nil
		end
	end
end

And for insert :
table.insert(CanBeSeen, {[v.Name] = v.Character.Head})

Also thx for trying helping @V_ladzec !

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