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 a certain item is in a player's inventory?

local Spawner = Vector3.new(157.324, 0.974, 26.163)
local Clicker = script.Parent 
local ObjectSpawn = script.Parent.Parent.Spawn 
local debounce = false

	
Clicker.MouseClick:Connect(function(player)
	if not debounce then
		debounce = true
	local Backpack = player:FindFirstChild("Backpack")
	if Backpack ~= nil then
		print "Check"
		local Obj = Backpack:GetChildren()
		if Obj == ("Bowl") then 
			local Object = game.ReplicatedStorage:findFirstChild("Bowl")
			print "Bowl"
			end
		if Obj == ("Classic") then
			local Object = game.ReplicatedStorage:findFirstChild("Classic")
			print "Classic"
		else
			local Object = game.ReplicatedStorage:findFirstChild("Cardboard")
			print "Cardboard"
		script.Parent.Parent.BrickColor = BrickColor.new("Persimmon")
		local CloneObject = Object:Clone()
	   	CloneObject.Parent = game.Workspace 
	   	CloneObject:MoveTo(Spawner)
		wait (12)
		script.Parent.Parent.BrickColor = BrickColor.new("Shamrock")
		debounce = false
		end
		end
	end
end)

This script is meant to check if a player has an item in their inventory, and if so, spawn it. For some reason though, it always spawns cardboard. It is printing check, so I have found the player's backpack, but when I put either "Bowl" or "Classic" in the starter pack it still spawns cardboard. Also, nothing else is being printed. Why?

16 comments

0

try to loop through the children of the backpack and see if any of the children's name matches what you're searching for

1

don't set the default of object to cardboard

0

So I changed it so that now the default of Object isn't Cardboard and then I get this error: Workspace.SledSpawn.ClickDetector.Script:26: attempt to index global 'Object' (a nil value)

0

I was able to fix the error using "else" and now it is printing cardboard as well. However, it always print cardboard and never recognizes what Obj is.

0

thats because in your script Obj is a table. update your script to what you currently have.

0

done

0

gotta fix that formatting, is bad

0

I know I'm sorry I just started

0

you need to loop through the table based on length of table, you cant just check a table against a string like that

0

How would I know the length of the table?

0

Oh...Well I figured it out and with Classic in the backpack it read: 1= Classic ... so what I'm searching for is the child of the backpack

0

#Obj would return the length of the table

0

Yeah, that's what I used to do it

0

for i, v in pairs(Obj) do if v.Name == ("whatever") --do stuff end // thats the jist of what you're looking for

0

Thanks! now it is printing the name of whatever is in my inventory

0

just make an if / elseif for the names and wham bam.

1 answer

✓ Accepted answer

You only spawn Cardboard because you're comparing Obj (which is a table) to a string, which will be false. On the else statement, you finally set Object to the Cardboard, so it will always spawn the cardboard. You can also use a for loop to reduce the number conditionals that you have to write.

local Spawner = Vector3.new(157.324, 0.974, 26.163)
local Clicker = script.Parent 
local ObjectSpawn = script.Parent.Parent.Spawn 
local debounce = false


Clicker.MouseClick:Connect(function(player)
	if not debounce then
		debounce = true

		local Backpack = player:FindFirstChild("Backpack")
    		if Backpack ~= nil then
        		print "Check"

			--I assume you want to spawn one random item from the player's inventory.
			local Item= {}			

local Obj = Backpack:GetChildren()
			
			for _,v in pairs(Obj) do
				print(v.Name)
				table.insert(item, v)
			end        		
			
			--If the player has the item then clone a random one to workspace.
			if #Item ~= 0 then
				local Object = game.ReplicatedStorage:FindFirstChild(Item[math.random(1,#Item)].Name)

				if Object then
					Clone = Object:Clone()
					Clone.Parent = workspace
					Clone:MoveTo(Spawner)
				end
			end
		
			script.Parent.Parent.BrickColor = BrickColor.new("Persimmon")
			wait(12)
			script.Parent.Parent.BrickColor = BrickColor.new("Shamrock")
			debounce = false

		end
	end
end)



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