Check players backpack for item

Hello, i was wondering if it would be possible to check the players backpack in a one player server as shown in image:
Screenshot 2021-10-21 at 9.48.36 AM

The scenario is in a if loop where only if the player contains a specific item in their backpack then the script does something like open a door or move a part. I would like to do this with a proximity prompt in a script.

How would i edit this script to make it detect for a specific item in the players inventory and then if they have it, it will preform the function?

(oops, sorry i uploaded the wrong image now ive changed it :sweat_smile: )

it would be great if you could comment an example.

-FastTheDev

1 Like
local Item = Backpack:WaitForChild("EPIC_TOOL")
print("FOUND THE EPIC_TOOL!")

You can use :WaitForChild to “suspend” the current thread until a tool named EPIC_TOOL is given to them.

You could also use ChildAdded if you need to detect it in an event styled fashion

Backpack.ChildAdded:Connect(function(Tool)
    if Tool.Name == "EPIC_TOOL" then
        print("FOUND THE EPIC_TOOL!")
    end
end)
2 Likes

If you had a script, it would be very useful if you published it, also, if it is to all players

local Target = "Sword" -- name of the object to search
function Check(Player:Player)
	if Player:FindFirstChild("Backpack") and Player.Backpack:FindFirstChild(Target) then
		-- Code
	end
end
for _, Player in pairs(game:GetService("Players"):GetPlayers()) do
	Check(Player)
end

if it’s only one

local Target = "Sword" -- name of the object to search
local Player = -- player in question
if Player:FindFirstChild("Backpack") and Player.Backpack:FindFirstChild(Target) then
	-- Code
end
2 Likes

thank you very much. this helped a lot : )

1 Like

What did you mean by this, what am i supposed to type in there. Could you please give an example?

if this is a server script, you would get the player using a playeradded event and set all code inside of it

game.Players.PlayerAdded:Connect(function(player)--now u have player
     --code
end)

now for a local script, its as simple as

local player = game.Players.LocalPlayer

basically, they are saying to put the player in there

2 Likes

This code wont work for some reason.
Why?
What do i need to fix?

local Target = “Sword” – name of the object to search
local Player = game.Players.PlayerAdded:Connect(function(player) – player in question (might delete this later)

local ProximityPrompt = script.Parent
local Door = script.Parent.Parent

ProximityPrompt.Triggered:Connect(function()
if Player:FindFirstChild(“Backpack”) and Player.Backpack:FindFirstChild(Target) then
– Code
if ProximityPrompt.ActionText == “Open” then
Door.Transparency = .5
Door.CanCollide = false
ProximityPrompt.ActionText = “Close”
else
Door.Transparency = 0
Door.CanCollide = true
ProximityPrompt.ActionText = “Open”
end
end

end)

local Target = “Sword” – name of the object to search
local Player = game.Players.PlayerAdded:Connect(function(player) – player in question (might delete this later)

local ProximityPrompt = script.Parent
local Door = script.Parent.Parent

ProximityPrompt.Triggered:Connect(function()
if Player:FindFirstChild(“Backpack”) and Player.Backpack:FindFirstChild(Target) then
– Code
if ProximityPrompt.ActionText == “Open” then
Door.Transparency = .5
Door.CanCollide = false
ProximityPrompt.ActionText = “Close”
else
Door.Transparency = 0
Door.CanCollide = true
ProximityPrompt.ActionText = “Open”
end
end

end)

Triggered returns the Player, use it

local Target = "Sword" -- name of the object to search
local ProximityPrompt = script.Parent
local Door = script.Parent.Parent

ProximityPrompt.Triggered:Connect(function(Player)
	if Player:FindFirstChild("Backpack") and Player.Backpack:FindFirstChild(Target) then
		-- Code
		if ProximityPrompt.ActionText == "Open" then
			Door.Transparency = .5
			Door.CanCollide = false
			ProximityPrompt.ActionText = "Close"
		else
			Door.Transparency = 0
			Door.CanCollide = true
			ProximityPrompt.ActionText = "Open"
		end
	end

end)

local Target = "Sword" -- name of the object to search
local ProximityPrompt = script.Parent
local Door = script.Parent.Parent

ProximityPrompt.Triggered:Connect(function(Player)
	if Player:FindFirstChild("Backpack") and Player.Backpack:FindFirstChild(Target) then
		-- Code
		if ProximityPrompt.ActionText == "Open" then
			Door.Transparency = .5
			Door.CanCollide = false
			ProximityPrompt.ActionText = "Close"
		else
			Door.Transparency = 0
			Door.CanCollide = true
			ProximityPrompt.ActionText = "Open"
		end
	end
end)

8 Likes

now that we do have a solution, a simple way to use playeradded is

game.Players.PlayerAdded:Connect(function(player)--player is now able to be used in event code
     --rest of code goes here :)
end)

so i just do this?

game.Players.PlayerAdded:Connect(function(player)--player is now able to be used in event code
       local Target = “Sword” – name of the object to search
local Player = game.Players.PlayerAdded:Connect(function(player) – player in question (might delete this later)

local ProximityPrompt = script.Parent
local Door = script.Parent.Parent

ProximityPrompt.Triggered:Connect(function()
if Player:FindFirstChild(“Backpack”) and Player.Backpack:FindFirstChild(Target) then
– Code
if ProximityPrompt.ActionText == “Open” then
Door.Transparency = .5
Door.CanCollide = false
ProximityPrompt.ActionText = “Close”
else
Door.Transparency = 0
Door.CanCollide = true
ProximityPrompt.ActionText = “Open”
end
end

end)
end)