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

How to check backpack to see if an item is there?

Asked by 6 years ago
Edited 6 years ago

I tried this but it says that Gather1 is not a valid member of backpack, so it breaks. I'm trying to make it so that you can't buy the item if you already have it. I also need this for when they want to sell something.

function onClick(hit)
if  game.Players.LocalPlayer.leaderstats.Gold.Value >= 5 and game.Players.LocalPlayer.Backpack.Gather1 == nil  then
game.Players.LocalPlayer.leaderstats.Gold.Value = game.Players.LocalPlayer.leaderstats.Gold.Value - 5
tool = game.ReplicatedStorage.Tools.Gather1:Clone()
tool.Parent = game.Players.LocalPlayer.Backpack
end
   end 
script.Parent.MouseButton1Down:connect(onClick)

3 answers

Log in to vote
0
Answered by
yoavstr 52
6 years ago

Hey, I haven't tested it but I think that should work:

function checkItemInInventory(item)
    for index, value in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
        if value.Name == item.Name then
            return false
        else
            return true
        end
    end
end

function onClick(hit)
    if  game.Players.LocalPlayer.leaderstats.Gold.Value >= 5 and  checkItemInInventory(game.Players.LocalPlayer.Backpack.Gather1)  then
    game.Players.LocalPlayer.leaderstats.Gold.Value = game.Players.LocalPlayer.leaderstats.Gold.Value - 5
    local tool = game.ReplicatedStorage.Tools.Gather1:Clone()
    tool.Parent = game.Players.LocalPlayer.Backpack
    end
       end
script.Parent.MouseButton1Down:connect(onClick)

What we are doing in this code is calling a function, the function checks if the item is in the inventory, if it is it return false (because we want it to work when the item isn't in the inventory) if it's not it returns true.

I'm not sure it will work because as I said I haven't tested it, so if you face any problem please respond and I will try to do my best to help you.

Have a nice day :)

0
Use more variables. User#19524 175 — 6y
0
When I attempt to buy it, I get the same message that it isn't in my backpack. And if I have a copy in my backpack, it doesn't have any errors, but it will give me another tool. Edbotikx 99 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You need to use FindFirstChild. It’s never a good idea to check the requirements on the server, because the client can manipulate the requirements to less. I suggest doing it on the server with a RemoteFunction. But do it however you can.

local plr = game:GetService("Players").LocalPlayer
local tool = game:GetService("ReplicatedStorage").Tools.Gather1

function onClick() -- MouseButton1Click has no parameters
    if plr.leaderstats.Gold.Value >= 5 then

        if not plr:FindFirstChild("Gather1", true) and not plr.Character:FindFirstChild"Gather1" then

            plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value - 5
            tool:Clone().Parent = plr.Backpack
            tool:Clone().Parent = plr.StarterGear
        end
    end
end

script.Parent.MouseButton1Click:Connect(onClick)
-- Switch to :Connect, :connect is deprecated

On a side note, switch to :Connect(), as ROBLOX may remove :connect() soon.

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
--sorry for using space I did this all on mobile
function findTool(player, toolname)
 if player then 
 player.Character.Humanoid:UnequipTools()
  for i, v in pairs(player.Backpack:GetChildren()) do 
   if v:IsA("Tool") and v.Name == toolname then 
     return true 
    end
  end
 end
end
 --use function like this
local HasTool = findTool(player, "Gather1")
if HasTool == true then 
 Print("has tool")
end

Answer this question