Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I check if an item is in the players backpack?

So, I want to only allow the player to exchange their tool for another one if they already have it in their inventory. I made a simple script which works, but you can just make the tool appear out of no where without having the thing you need to craft it. I don't know if i'm just being stupid because it sounds so simple but im new to programming and having difficulty.

script.Parent.Triggered:Connect(function(player)
	
	local Pack = player.Backpack
	local rs = game:GetService("ReplicatedStorage")
	local Item = rs:WaitForChild("Copper")
	local ItemClone = Item:Clone()
	ItemClone.Parent = Pack
	local RemoveItem = "Malachite Chunk"
	
	if player.Character and player.Character:FindFirstChild(RemoveItem) and player.Character[RemoveItem]:IsA("Tool") then
		player.Character[RemoveItem]:Destroy()
	end
end)

This script is inside a proximity prompt, and maybe there is a way for the proximity prompt to just not show up if they don't have it in their inventory i'm thinking?

1 answer

✓ Accepted answer

You would check the player's backpack instead of the player's character.

player.Character:FindFirstChild(RemoveItem) --instead of this

player.Backpack:FindFirstChild(RemoveItem) --you would do this

Let me know if this helps

Since you want to disable the prompt if they don't have the item in their inventory, I made this localscript.

local player = game:GetService("Players").LocalPlayer
local char = player.Character
local prompt = game.Workspace.test.Attachment.ProximityPrompt --change this to your proximity prompt
while wait(0.1) do
if player.Backpack:FindFirstChild("Flash Light") or player.Character:FindFirstChild("Flash Light") then -- checking the player's backpack incase they dont have it equipped, and the character incase they do
	prompt.Enabled = true --enabling the prompt if they have the item
else
	prompt.Enabled = false -- disabling if they dont
	end 
	end

Change the flash light to the item you want, the localscript goes in starterplayerscripts. Hope this helps!

3 comments

0

I have tried this but it does remove it from the backpack, but not from the toolbar from the bottom. My solution was to use to check the character and it worked, but i''m just wondering if I can only allow the script to run if a certain item is in the backpack already

0

I've added something to my answer, check it out and get back to me if it helps you

0

Thank you so much it works perfectly!!!

This discussion is preserved as it was. New answers, comments, and votes are closed.