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

How to Make already have item object?

Asked by
Games_DBZ -15
3 years ago
Edited 3 years ago

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)


1
you should use variables, instead of saying "script.Parent.Parent.Parent.Parent.Parent.Parent.Parent" and so on niroqeo 123 — 3y

3 answers

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
3 years ago
Edited 3 years ago

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
0
Works!! Thanks. Games_DBZ -15 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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)
Log in to vote
0
Answered by 3 years ago

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!

0
not work! Games_DBZ -15 — 3y
0
What it is not working? BestCreativeBoy 1395 — 3y

Answer this question