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!
How do I check a players inventory/backpack for a specific tool?
2 answers
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
Thank You Very Much!
I have a question though! Here is my code:
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)
It doesn't seem to work!? Any ideas why?
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.

2 comments
can you include your script in a code block
When you equip the tool you can find it inside of the character?