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

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

Asked by 5 years ago

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!

0
can you include your script in a code block User#5423 17 — 5y
0
When you equip the tool you can find it inside of the character? Lava_Scripter 109 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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)
0
Thank You Very Much! Crystalgamesstudio20 4 — 5y
0
I have a question though! Here is my code: Crystalgamesstudio20 4 — 5y
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) Crystalgamesstudio20 4 — 5y
0
It doesn't seem to work!? Any ideas why? Crystalgamesstudio20 4 — 5y
0
Never mind, solved! Thanks very much! Crystalgamesstudio20 4 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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

Answer this question