Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I detect if target is in table?

local selected = {}

mouse.Button1Down:Connect(function()
	local team = game.Workspace:WaitForChild(player.TeamColor.Name)
	if team then
		if mouse.Target:IsDescendantOf(team) then		
			table.insert(selected,mouse.Target.Parent)
			print(unpack(selected))
			end
		end
end)

Basically if what is selected is member of team it inserts it into the table but when you select it again it inserts a second copy into the table. How can I see if its already in the table? thanks.

1 answer

✓ Accepted answer

make a loop that goes through all the table values(or copy my code):

local function checkintable(table, object) -- put in your table and the object you are looking for
	for i = 1, #table do -- change the one to zero if your first place in the array is at 0
		if  table[i] == object then
			return true
		end
	end
	return false -- make a variable that calls this function and then add an "if then" statement saying if it returns false, then add it into the table.
end)

4 comments

0

Thx

0

Did it work?

0

Did it work?

0

Sorry for not replying. It worked great!

This discussion is preserved as it was. New answers, comments, and votes are closed.