Lets say I have a table, with 1 element. Then I insert another element into the table. I want to check if table has more than one element, I delete the 1st element and replace it with the second. I know how to remove elements from tables and add, but I don’t know how to check if it has more than 1 element in it. Do I use table.find()?
You could do table.find to find another element if you want
Though you could also learn OOP in this case with index and stuff. Making metatables
You can use # operator:
local tab = {"apple", "bannana"}
print(#tab) --> 2
I think you want the length of the table, which you can get by doing
#exampletable
In you case it would be
if #exampletable > 1 then
exampletable[1] = exampletable[2]
exampletable[2] = nil
end
This would result in a lot of if statements if he did this
He would need to insert the stuff in the table manually I think he’s inserting it through scripts then trying to find it, etc in which case table.find is his solution if that is true.
Uh no? If you want to shift everything over 1 place
for i = 1, #table do
table[i] = table[i+1]
end
(As others have mentioned)
The #
literally gets the amount of elements (Or length depending on how you look at it) in a table, table.find
should only be necessary if you want to check for a certain value to find while #Table
can check if you’re excluding the amount of objects in a table:
local Table = {"I am a string", 50, true, workspace.Baseplate}
print(#Table)
if #Table > 1 then
print("Yes")
end
For this:
Just use the table.insert/remove
functions to add/remove what you want
local Table = {"I am a string"}
table.insert(Table, "I am yet another string")
print(#Table)
if #Table > 1 then
print("Yes")
end
table.remove automatically shifts everything, use that. It should be faster than anything you can write.
if #table > 1 then
--code
end