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

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

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

✓ Accepted answer

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

5 comments

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

0

i edited my answer

0

Thank you I'll try this!

0

Yeah I'm either doing it wrong or it doesn't work

0

how does the acceptedCards list look?

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