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:
1 | local ignoreList = { "Part" , "Base" , "BasePlate" } |
2 | local a = ignoreList |
3 | if spot.ClassName = = "Part" and spot.Name ~ = ignoreList [ #a ] then |
4 | -- code |
5 | end |
I basically want the code to be more effecient. Since I don't want to write this:
1 | if spot.ClassName = = "Part" and spot.Name ~ = "etc" or spot.Name ~ = "etc" or spot.Name ~ = "etc" then |
2 | --code |
3 | end |
You could use string keys, it would be an easier way.
For example:
1 | local ignoreList = { [ "Part" ] = true , [ "Base" ] = true , [ "BasePlate" ] = true } |
2 |
3 | -- :IsA("BasePart") returns true for parts, wedges, unions and is much better than using ClassName |
4 | if (spot:IsA( "BasePart" ) and (ignoreList [ spot.Name ] = = nil )) then -- Check the ignoreList for that string key |
5 | -- code |
6 | end |
Alternatively you can loop through the ignoreList and check against each value.
01 | local ignoreList = { "Part" , "Base" , "BasePlate" } |
02 |
03 | if (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 |
14 | end |