Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Working with tables?

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

✓ Accepted answer

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 comment

1

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.