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

Checking a value in a part against a table?

What I'm trying to do is make a card access door, which when you touch your card against the scanner, it checks a value inside the card and if it is in "access" it unlocks a door.

local access = {
	001,	
}
script.Parent.Touched:connect(function(part)
	if part.Parent:FindFirstChild("CardNumber") ~= nil then
		if access[part.Parent.CardNumber.Value] then
			local door = script.Parent.Parent.Parent.Door:GetChildren()
			script.Parent.Parent.Card.Transparency = 0
			script.Parent.Parent.Card.DecalA.Transparency = 0
			script.Parent.Parent.Card.Decal.Transparency = 0
			for i,v in pairs(door) do
				v.CanCollide = false
			end
			wait(3)
			script.Parent.Parent.Card.Transparency = 1
			script.Parent.Parent.Card.DecalA.Transparency = 1
			script.Parent.Parent.Card.Decal.Transparency = 1
			for i,v in pairs(door) do
				v.CanCollide = true
			end
		end
	end
end)

There are no errors nor output.

4 comments

0

to search if one of the numbers in the table is in the card, you need to use a loop to search the whole table

2

you:"There are no errors nor output." me:"ever think to use print?"

0

I agree^

0

^ Well normally there'd be output, just saying...

1 answer

✓ Accepted answer

Dictionaries

The issue with how you have it set up is that you want to check if a table contains something else. Lua doesn't have an inherent way to do that, but what you can do instead is test to see if the key matches to a value.

Consider a table where you have your card numbers as keys instead:

allow = {
	[002] = true;
	[003] = true;
	[005] = true;
	[007] = true;
	[011] = true;
	[013] = true;
}

In this case, you can check if your card is allowed by doing what you're already doing, because it will either resolve to true or nil.

You must make sure that you're using the same type of value for your key. If the value you're trying to check for is a string, make sure that the keys are strings. Lua equality means that different datatypes are different.

2 comments

0

I'm assuming you want e.g. ["002"] = true instead of [002] = true so that the 0s are significant?

0

I just wrote it in a way that's meant to make sense to him. It's what he used, so I'm going to use it too - Don't fix what's not broken.

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