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

Checking a value in a part against a table?

Asked by 8 years ago

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.

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 lukeb50 631 — 8y
2
you:"There are no errors nor output." me:"ever think to use print?" LostPast 253 — 8y
0
I agree^ User#11440 120 — 8y
0
^ Well normally there'd be output, just saying... TheHospitalDev 1134 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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.

0
I'm assuming you want e.g. `["002"] = true` instead of `[002] = true` so that the 0s are significant? BlueTaslem 18071 — 8y
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. User#6546 35 — 8y
Ad

Answer this question