How to find an item that can be in two different spots?

Hey Devs,

I am looking for a way to find a tool that can either be in the player’s Backpack, or inside of their character (if it is equipped); during an event.

Basically, I am wanting to make a change to the tools color, based on the characters actions. The only problem, is that I cannot find a way to write the code so that it check the Backpack, and if the tool is not there, then it checks the Character.

This is the code I currently have, but it is not working; probably because I’m new to pcalls:

local player = game.Players.LocalPlayer

local button = script.Parent

local prompt = script.Parent.Parent.Parent

local item = prompt.ItemName

button.MouseButton1Click:Connect(function()
	
	local pclone = prompt:FindFirstChild("Phone")
	
	local phone
	
	pcall(function(succ,err)
		phone = player.Backpack.Phone
		
		if err then
			phone = player.Character.Phone
			print(err)
		end
		
	end)
	
	if phone then 
		print("Gotta phone")
	end
	
end)

The “if err then” statements need to be outside the pcall:

	local succ,err = pcall(function()
		phone = player.Backpack.Phone
	end)

	if err then
		phone = player.Character.Phone
		print(err)
	end
1 Like

That’s not what pcall is for, you can simply use FindFirstChild instead to check if ‘Phone’ is a child of backpack or character. pcalls are often used in situations where you don’t know if the code will work as expected, for example http requests - you are not 100% sure that the server will respond.

1 Like