How can I find things that aren't in the table?

I am making a tycoon sort of game. When you leave the game checks what you bought and what you haven’t and saves them into 2 separate tables. When the player rejoins and touches the door, the tycoon should load. It works fine, but because I make it so when the players buy an item, the button gets destroyed so he or she won’t accidentally spend money the thing the player bought. But because it gets deleted, it’s not in the table.
So when the player joins, even if the player bought it, the button would still be there.
Is there a way to check if an object’s Name is in a table and if it isn’t then it gets destroyed?
I tried to use for i,v in pairs but I don’t know how you can check if its Name is in the table.

1 Like
for i,v in pairs(tabl) do
	if v == name then
		found = true
	end
end

if found then 
	print("In table")
else
	print("Not in Table")
end

This script checks If name is in table.

2 Likes

If it’s an array

local t = {1, 2, 3}
if not table.find(t, 4) then print("4 not found") end

-- in case it's a dictionary
-- the table library is largely and probably only relevant to arrays

local t = {
Number1 = "10",
Number2 = "20",}

if not t["Number3"] then print("Number 3 has no value associated with it") end
2 Likes

What if I have to check if it’s in both tables? If it’s just in one then it’s ok but if it’s not in both tables then it gets destroyed. How would I do that?

Wouldn’t it be faster to just use the find method of the table library, as @XxELECTROFUSIONxX suggested?

1 Like

It definitely would, but the thing is it won’t work for dictionaries as I mentioned, the table library is irrelevant to dictionaries totally.

You can still use convoluted checks to determine whether a table is numerically indexed or not, and search for indexes or values in it using a suitable iterator.

2 Likes

I didn’t use dictionaries, but how would I do it if I want to know if it’s not in both tables?
Do i simple do table.find() twice? But if a part is in table 1 and not in table 2 then won’t it be deleted?

Then essentially yes, just use table.find to check whether a numeric index has a certain value associated in whatever number of tables you want.

I don’t get it.
Let’s say I have 2 tables.

local t1 = {1, 2, 3}
local t2 = {5, 6, 7}
if not table.find(t1, 4) then print("4 not found in table one.") end
if not table.find(t2, 4) then print ("4 not found in table two.") end

But what if I would delete it if it doesn’t exist?

local t1 = {1, 2, 3}
local t2 = {5, 6, 7}
if not table.find(t1, 4) then print('What do I do here'))end
if not table.find(t2, 4) then print('What do I do here')end

I want to do workspace:FindFirstChild(4):Destroy() if 4 doesn’t exist in both scripts.

1 Like

You can’t delete something that doesn’t exist, what are you saying??

If you meant you need to destroy something (not an element in the table obviously) if it doesn’t exist, then

local t1 = {"NotPart"}
local t2 = {"Not a  part"}

local name = "Part"
local find = table.find
if not (find(t1, name) and find(t2, name)) then 
   print("not in both tables")
   workspace[name]:Destroy()
end

If its Name doesn’t exist in the table, it would be deleted form the workspace.

found1 = false
found2 = false

for i,v in pairs(table1) do
	if v == name then
		found1 = true
	end
end


for i,v in pairs(table2) do
	if v == name then
		found2 = true
	end
end



if found1 and found2 then 
	-- it is in both tables
else
	-- it is not in both tables
end
1 Like

Because I’m checking almost up to 100 items, I don’t think table.find() is efficient.
I may be wrong though.

1 Like

There’s nothing else you can do other than essentially replicate the function trying to do what it does, it’s as efficient as it can be already.

It probably looks like this


local Table = {}
local function Table.find(t, v)
   for i = 1, #t do
      if t[i] == v then return true
   end
end