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.
1 | function onClick(hit) |
2 | if game.Players.LocalPlayer.leaderstats.Gold.Value > = 5 and game.Players.LocalPlayer.Backpack.Gather 1 = = nil then |
3 | game.Players.LocalPlayer.leaderstats.Gold.Value = game.Players.LocalPlayer.leaderstats.Gold.Value - 5 |
4 | tool = game.ReplicatedStorage.Tools.Gather 1 :Clone() |
5 | tool.Parent = game.Players.LocalPlayer.Backpack |
6 | end |
7 | end |
8 | script.Parent.MouseButton 1 Down:connect(onClick) |
Hey, I haven't tested it but I think that should work:
01 | function 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 |
09 | end |
10 |
11 | function onClick(hit) |
12 | if game.Players.LocalPlayer.leaderstats.Gold.Value > = 5 and checkItemInInventory(game.Players.LocalPlayer.Backpack.Gather 1 ) then |
13 | game.Players.LocalPlayer.leaderstats.Gold.Value = game.Players.LocalPlayer.leaderstats.Gold.Value - 5 |
14 | local tool = game.ReplicatedStorage.Tools.Gather 1 :Clone() |
15 | tool.Parent = game.Players.LocalPlayer.Backpack |
16 | end |
17 | end |
18 | script.Parent.MouseButton 1 Down: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 :)
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.01 | local plr = game:GetService( "Players" ).LocalPlayer |
02 | local tool = game:GetService( "ReplicatedStorage" ).Tools.Gather 1 |
03 |
04 | function 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 |
14 | end |
15 |
16 | script.Parent.MouseButton 1 Click:Connect(onClick) |
17 | -- Switch to :Connect, :connect is deprecated |
:Connect()
, as ROBLOX may remove :connect()
soon.01 | --sorry for using space I did this all on mobile |
02 | function 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 |
11 | end |
12 | --use function like this |
13 | local HasTool = findTool(player, "Gather1" ) |
14 | if HasTool = = true then |
15 | Print( "has tool" ) |
16 | end |