Where can I find and get a tool when it is equipped?

Hello developers!

I have hit a road block. I have a script that detects if a player has a tool in there backpack, and if they do then it runs code. But if the player equippeds the tool it gives me a error. What I don’t know is how can I find that tool when it is equipped.

My code

Script

PromptLeft.Triggered:Connect(function(player)
	local success, Error = pcall(function()
		if player.Backpack.Keycard then
			if Debounce == false then
				KeycardSuccess2:Play()
				KeycardSuccess2.Ended:Wait()
				DoorLeftOpen:Play()
				DoorLeftOpen.Completed:Wait()
				Debounce = true
			else
				KeycardSuccess2:Play()
				KeycardSuccess2.Ended:Wait()
				DoorLeftClose:Play()
				DoorLeftClose.Completed:Wait()
				Debounce = false
			end
		end
	end)
	if Error then
		KeycardFailSound1:Play()
		KeycardFailSound1.Ended:Wait()
	end
end)

3 Likes
PromptLeft.Triggered:Connect(function(player)
	local success, Error = pcall(function()
		if player.Backpack.Keycard or player.Character.Keycard  then
			if Debounce == false then
				KeycardSuccess2:Play()
				KeycardSuccess2.Ended:Wait()
				DoorLeftOpen:Play()
				DoorLeftOpen.Completed:Wait()
				Debounce = true
			else
				KeycardSuccess2:Play()
				KeycardSuccess2.Ended:Wait()
				DoorLeftClose:Play()
				DoorLeftClose.Completed:Wait()
				Debounce = false
			end
		end
	end)
	if Error then
		KeycardFailSound1:Play()
		KeycardFailSound1.Ended:Wait()
	end
end)

When a player equips any tool, the parent of the tool will set to the player character

When a player equips a tool, it gets parented to their character

This still gives me a error. Thanks for the reply tho!

try using findfindfirstcchild funcction

If I do it like this I still get a error

player.Character:FindFirstChild("Keycard")

Can you send the error message?

It’s inside of a pcall but if I remove pcall I get this

Keycard is not a valid member of Backpack "Players.Koolkp5.Backpack"

You could swap to :WaitForChild and make sure the name is right

Still get a error same as before

Are you sure the error is not from another part of the script, or from the item name being spelled and capitalized wrong?

I checked and I don’t think so. I am getting the Keycard from ReplicatedStorage and cloning it to the player backpack
Screenshot

player.Character:WaitForChild("Keycard")

Found the error. My code just in case if somebody needs some help.

Button.MouseButton1Click:Connect(function(player)
	local Success, Error pcall(function()
		if player.Character.Keycard then
			print("Yay")
			-- Code
		end
	end)
	local success, erro pcall(function()
		if player.Backpack.Keycard then
			print("Keycard")
			-- Code
		end
	end)
end)
1 Like

hi, can u please mark my post as a solution. I think it’s a bit selfish of you to mark your own post as a solution.