Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I detect if target is in table?

Asked by 5 years ago
01local selected = {}
02 
03mouse.Button1Down:Connect(function()
04    local team = game.Workspace:WaitForChild(player.TeamColor.Name)
05    if team then
06        if mouse.Target:IsDescendantOf(team) then      
07            table.insert(selected,mouse.Target.Parent)
08            print(unpack(selected))
09            end
10        end
11end)

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

Log in to vote
0
Answered by 5 years ago

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

1local function checkintable(table, object) -- put in your table and the object you are looking for
2    for i = 1, #table do -- change the one to zero if your first place in the array is at 0
3        if  table[i] == object then
4            return true
5        end
6    end
7    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.
8end)
0
Thx THEROBLOXAIN2 15 — 5y
0
Did it work? greenhamster1 180 — 5y
0
Did it work? greenhamster1 180 — 5y
0
Sorry for not replying. It worked great! THEROBLOXAIN2 15 — 4y
Ad

Answer this question