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

How do I check a players inventory/backpack for a specific tool?

I am trying to make a prison game and need to make doors! I already scripted a door that requires you to touch the door with a specific tool named “Keycard”. But I want the door to work both when you touch it with the Keycard and when you have a Keycard in your inventory or backpack!

2 comments

0

can you include your script in a code block

0

When you equip the tool you can find it inside of the character?

2 answers

✓ Accepted answer

A easy way of doing this is to use or and FindFirstChild(Obj)

local Key = Character:FindFirstChild("Keycard")or Player.Backpack:FindFirstChild("Keycard")
Door.Touched:Connect(function(Hit)
if Key then
--Code here
end)

5 comments

0

Thank You Very Much!

0

I have a question though! Here is my code:

0

local Key = Character:FindFirstChild("Keycard")or Player.Backpack:FindFirstChild("Keycard") Door.Touched:Connect(function(hit) if Key then script.Parent.Transparency = 0.5 script.Parent.CanCollide = false wait(1) script.Parent.Transparency = 0 script.Parent.CanCollide = true end)

0

It doesn't seem to work!? Any ideas why?

0

Never mind, solved! Thanks very much!

the FindFirstChild() method of the Instance class can be used to get a reference to a child instance bearing the name passed as it's first argument. If an instance by this name does not exist as a child of the instance the method was called on, it will return nil.

local keyCard = character:FindFirstChild('KeyCard')

if not keyCard then
	keyCard = player.Backpack:FindFirstChild('KeyCard')
	
	if not keyCard then
		-- at this point we can confirm they are neither holding a keycard, nor have one in their backpack
		return
	end
end

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