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

Detecting an item within a list the player has (in backpack or character)?

Asked by 2 years ago

I want my door to accept variety of tools (ex. L1, L2, L3) whether it resides in their backpack or their character.

I tried this,

function open(player)
        local card = player.Backpack:FindFirstChild(acceptedCards) or player.Character:FindFirstChild(acceptedCards)
        local other = player.Backpack:FindFirstChild(otherCards) or player.Character:FindFirstChild(otherCards)
        if card then
            (opens)
        elseif other then
            (opens but in different way so it's important)
        else
            (denied)
        end
    end
end

but that only worked when acceptedCards was only one item when I want it to be multiple items which is the problem. Please help and thank you!

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

do you mean something like this?

if card and other then
    print("both")
elseif card then
    print("only card")
elseif other then
    print("only other")
else
    print("denied")
end

Think of and as you read it, if this card and other card exists then blah blah blah

EDIT: You loop through the acceptedCards table and check FindFirstChild for each of it;

local function isAccepted(player, cards)
    for _, card in ipairs(cards) do
        if player.Character:FindFirstChild(card) or player.Backpack:FindFirstChild(card) then return true end
    end

    return false
end

[...]

if isAccepted(player, acceptedCards) then
    (opens)
elseif isAccepted(player, otherCards) then
    (opens but in different way so it's important)
else
    (denied)
end

Read about ipairs and pairs if you don't know what it is here

0
Sorry what I want is for the door to accept one of the acceptedCards which is supposed to be a list of names that can be accepted. The problem is the script I have right now only works if acceptedCards or otherCards is equal to one name. I want it to work if acceptedCards is a list or multiple names FoxAviator24 2 — 2y
0
i edited my answer imKirda 4491 — 2y
0
Thank you I'll try this! FoxAviator24 2 — 2y
0
Yeah I'm either doing it wrong or it doesn't work FoxAviator24 2 — 2y
0
how does the acceptedCards list look? imKirda 4491 — 2y
Ad

Answer this question