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

Working with tables?

Asked by 9 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:

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

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

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

1 answer

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

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

For example:

1local ignoreList = {["Part"] = true, ["Base"] = true, ["BasePlate"] = true}
2 
3-- :IsA("BasePart") returns true for parts, wedges, unions and is much better than using ClassName
4if (spot:IsA("BasePart") and (ignoreList[spot.Name] == nil)) then -- Check the ignoreList for that string key
5-- code
6end

Wiki link

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

01local ignoreList = {"Part","Base","BasePlate"}
02 
03if (spot:IsA("BasePart")) then
04    local ignore = false
05    for _, ignoreValue in pairs(ignoreList) do -- loop through the table
06        if (spot.Name == ignoreValue) then -- Check the table value against spot.Name
07            ignore = true   -- if a match set ignore to true and break the for loop
08            break
09        end
10    end
11    if (not ignore) then -- if we had no matches run the code
12        --code
13    end
14end
1
Thank you! GeezuzFusion 200 — 9y
Ad

Answer this question