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.

1function onClick(hit)
2if  game.Players.LocalPlayer.leaderstats.Gold.Value >= 5 and game.Players.LocalPlayer.Backpack.Gather1 == nil  then
3game.Players.LocalPlayer.leaderstats.Gold.Value = game.Players.LocalPlayer.leaderstats.Gold.Value - 5
4tool = game.ReplicatedStorage.Tools.Gather1:Clone()
5tool.Parent = game.Players.LocalPlayer.Backpack
6end
7   end
8script.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:

01function checkItemInInventory(item)
02    for index, value in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
03        if value.Name == item.Name then
04            return false
05        else
06            return true
07        end
08    end
09end
10 
11function onClick(hit)
12    if  game.Players.LocalPlayer.leaderstats.Gold.Value >= 5 and  checkItemInInventory(game.Players.LocalPlayer.Backpack.Gather1then
13    game.Players.LocalPlayer.leaderstats.Gold.Value = game.Players.LocalPlayer.leaderstats.Gold.Value - 5
14    local tool = game.ReplicatedStorage.Tools.Gather1:Clone()
15    tool.Parent = game.Players.LocalPlayer.Backpack
16    end
17       end
18script.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.

01local plr = game:GetService("Players").LocalPlayer
02local tool = game:GetService("ReplicatedStorage").Tools.Gather1
03 
04function onClick() -- MouseButton1Click has no parameters
05    if plr.leaderstats.Gold.Value >= 5 then
06 
07        if not plr:FindFirstChild("Gather1", true) and not plr.Character:FindFirstChild"Gather1" then
08 
09            plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value - 5
10            tool:Clone().Parent = plr.Backpack
11            tool:Clone().Parent = plr.StarterGear
12        end
13    end
14end
15 
16script.Parent.MouseButton1Click:Connect(onClick)
17-- 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
01--sorry for using space I did this all on mobile
02function findTool(player, toolname)
03 if player then
04 player.Character.Humanoid:UnequipTools()
05  for i, v in pairs(player.Backpack:GetChildren()) do
06   if v:IsA("Tool") and v.Name == toolname then
07     return true
08    end
09  end
10 end
11end
12 --use function like this
13local HasTool = findTool(player, "Gather1")
14if HasTool == true then
15 Print("has tool")
16end

Answer this question