Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I check if a certain item is in a player's inventory?

Asked by 5 years ago
Edited 5 years ago
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?

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 theking48989987 2147 — 5y
1
don't set the default of object to cardboard SteamG00B 1633 — 5y
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) KingCheese13 21 — 5y
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. KingCheese13 21 — 5y
View all comments (12 more)
0
thats because in your script Obj is a table. update your script to what you currently have. DinozCreates 1070 — 5y
0
done KingCheese13 21 — 5y
0
gotta fix that formatting, is bad DinozCreates 1070 — 5y
0
I know I'm sorry I just started KingCheese13 21 — 5y
0
you need to loop through the table based on length of table, you cant just check a table against a string like that DinozCreates 1070 — 5y
0
How would I know the length of the table? KingCheese13 21 — 5y
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 KingCheese13 21 — 5y
0
#Obj would return the length of the table DinozCreates 1070 — 5y
0
Yeah, that's what I used to do it KingCheese13 21 — 5y
0
for i, v in pairs(Obj) do if v.Name == ("whatever") --do stuff end // thats the jist of what you're looking for DinozCreates 1070 — 5y
0
Thanks! now it is printing the name of whatever is in my inventory KingCheese13 21 — 5y
0
just make an if / elseif for the names and wham bam. DinozCreates 1070 — 5y

1 answer

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

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)



Ad

Answer this question