How Do You Find Something In A Table?

So lets just say I have one table and lets call it targets. Now I want to add a player name to targets. So I add in a player name to targets every time someone hovers over a Click detector. Now I don’t want it so that their name gets added over and over again to the table if they keep hovering over it. I want it to check if their name is already in the table. How Do I Achieve This? Sorry if I’m asking a stupid question its just something I can’t seem to figure out.

If You Know Anything About This Feel Free To Reply!

you can find out by doing basic research please refrain from posting before you do any research
Anyway here it is :

table.find(table,name)
-- example :
if table.find(targets,name) then
    -- the name is in the table already
 end
1 Like

It’s simple just try this:

local targets = {} -- Target table

local clickDetector = script.Parent.ClickDetector -- Click detector

local function hoveringMouse(player)
	if player.Name ~= table.unpack(targets) -- This tries to find player name in [target] table then
		table.insert(targets,player.Name) -- This inserts player's name in target list
		print(table.unpack(targets))
	else
		return
	end
end

clickDetector.MouseHoverEnter:Connect(hoveringMouse)

This does not work I don’t know why and when I remove the if statement it works again. There were no errors btw.
Here are some screenshots:
(without if statement)
image
(with if statement)
image

1 Like

Thank you for answering my stupid quarry. The reason why I posted this was because the
table.find was not working and I did not really think of using table.unpack

Thanks

1 Like

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