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

Working with tables?

Asked by 8 years ago

So I'm making an ignoreList by making a table and I'm trying to make my script more efficient. Im trying to get the code to ignore everything on 'ignoreList' table. I don't really know how tables work but here's what I got:

local ignoreList = {"Part","Base","BasePlate"}
local a = ignoreList
if spot.ClassName == "Part" and spot.Name ~= ignoreList[#a] then
-- code
end

I basically want the code to be more effecient. Since I don't want to write this:

if spot.ClassName == "Part" and spot.Name ~= "etc" or spot.Name ~= "etc"  or spot.Name ~= "etc" then
--code
end

1 answer

Log in to vote
3
Answered by
DevSean 270 Moderation Voter
8 years ago

You could use string keys, it would be an easier way.

For example:

local ignoreList = {["Part"] = true, ["Base"] = true, ["BasePlate"] = true}

-- :IsA("BasePart") returns true for parts, wedges, unions and is much better than using ClassName
if (spot:IsA("BasePart") and (ignoreList[spot.Name] == nil)) then -- Check the ignoreList for that string key
-- code
end

Wiki link

Alternatively you can loop through the ignoreList and check against each value.

local ignoreList = {"Part","Base","BasePlate"}

if (spot:IsA("BasePart")) then
    local ignore = false
    for _, ignoreValue in pairs(ignoreList) do -- loop through the table
        if (spot.Name == ignoreValue) then -- Check the table value against spot.Name
            ignore = true   -- if a match set ignore to true and break the for loop
            break
        end
    end
    if (not ignore) then -- if we had no matches run the code
        --code
    end
end


1
Thank you! GeezuzFusion 200 — 8y
Ad

Answer this question