Table.find() returning nil when shouldn't

My duel invite system adds both player’s names to a table like this:

-- plr is player1
--otherPlr is player2
table.insert(pendingInvites,{plr.Name,otherPlr.Name})

when using table.find() to check if the invite is still pending:

if table.find(pendingInvites,{plr.Name,otherPlr.Name}) then

the if statement doesn’t fire, meaning that table.find() returns nil.

This is the full code snippet:

sendInvite.OnServerEvent:Connect(function(plr,otherPlr : Player)
	-- plr is player1
	--otherPlr is player2
	table.insert(pendingInvites,{plr.Name,otherPlr.Name})
	print(pendingInvites)
	sendInvite:FireClient(otherPlr,plr)
	task.wait(10)
	print("passed wait(10)")
	print(pendingInvites)
	print(table.find(pendingInvites,{plr.Name,otherPlr.Name}))
	if table.find(pendingInvites,{plr.Name,otherPlr.Name}) then
		table.remove(pendingInvites,table.find(pendingInvites,{plr.Name,otherPlr.Name}))
		print(otherPlr)
		print(plr)
		declinerequest:FireClient(plr)
	end
end)

NOTE: Later on in my code table.find() actually works. the code is the same.

{} is not the same as {}. Loop through your pendingInvites and perform table.find or manually check on those tables.

Like this?

for _,i in pairs(pendingInvites) do
		if table.find(i,{plr.Name,otherPlr.Name}) then
			table.remove(pendingInvites,table.find(i,{plr.Name,otherPlr.Name}))
			declinerequest:FireClient(plr)
		end
	end

Nevermind. Figured it out.

if i[1] == plr.Name and i[2] == otherPlr.Name then	

sorry for that.

No. When you are passing in a newly constructed table, despite it being identical, their memory address is different and they are different tables.

print({} == {}) -- false

local myTable = {1, 2}
print({1, 2} == myTable) -- false

local pendingInvites = {{"player1", "player2"}}
table.find(pendingInvites, {"player1", "player2"}) -- nil

Assuming the structure of your table is {player, player}:

for _, pendingInvite in pendingInvites do
    if pendingInvite[1] == plr and pendingInvite[2] == otherPlayer then
        -- do stuff
    end
end
2 Likes

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