Hello, I would like to know how do I remove clones from the tool in the backpack, for example, once I already have the item, when I click to buy an object it is cloning in the back pack. Could someone help me?
function buy() if script.Parent.Parent.Parent.Parent.Parent.Parent.leaderstats.Level.Value >= 45000 then script.Parent.Parent.Parent.Parent.Parent.Parent.leaderstats.Level.Value = script.Parent.Parent.Parent.Parent.Parent.Parent.leaderstats.Level.Value-0 local clone1 = game.ServerStorage.Sword:Clone() clone1.Parent = script.Parent.Parent.Parent.Parent.Parent.Parent.Backpack end end script.Parent.MouseButton1Down:connect(buy)
Check the iten existence with a for loop:
local AlreadyHave = false for i, v in pairs (Player.Backpack:GetChildren()) do if v.Name == Item.Name then AlreadyHave = true end end if not AlreadyHave then -- do stuff end
It would really help if you used variables as mentioned above, making your code readable.
What you want to do to prevent multiple clones, is to use a debounce and a for loop to iterate through the player's backpack to see if the "Sword" already exists.
local player = game.Players.LocalPlayer local Backpack = player:FindFirstChild("Backpack") local Item = game:GetService("ServerStorage").Sword local debounce = false -- Local debounce variable script.Parent.TextButton.MouseButton1Click:Connect(function() if debounce == false then -- Only runs if debounce is false Item:Clone().Parent = Backpack end for i,v in pairs(Backpack:GetChildren()) do -- For loop through children of backpack if v.Name == "Sword" then -- Check to see if there's an item called Sword debounce = true -- Debounce set to true, so the above code wouldn't run end end end)
Well, I am going to provide you a solution with what almost everyone said. It is like looping through every item in your backpack.
function buy() local var = script.Parent.Parent.Parent.Parent.Parent.Parent -- Creating a variable for simplification local PlayerBackpack = var.Backpack:GetChildren() -- Creates a table with contents of the backpack if var.leaderstats.Level.Value >= 45000 then for i, tool in pairs(PlayerBackpack) do -- Runs a loopin the table created above if tool:IsA("Tool") then -- Checks if the item is a Tool then if tool.Name == "Sword" then -- Checks if the tool's name is Sword (eg) var.leaderstats.Level.Value = var.leaderstats.Level.Value - 0 local clone1 = game.ServerStorage.Sword:Clone() clone1.Parent = var.Backpack end end end end end script.Parent.MouseButton1Down:Connect(buy) -- Make sure Capital 'C'
Lemme know if it helps!