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!
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)
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