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 4 years ago
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

Log in to vote
0
Answered by 4 years ago

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)
0
Thx THEROBLOXAIN2 15 — 4y
0
Did it work? greenhamster1 180 — 4y
0
Did it work? greenhamster1 180 — 4y
0
Sorry for not replying. It worked great! THEROBLOXAIN2 15 — 3y
Ad

Answer this question