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

Weird gun glitch when I try to clone tool to backpack?

Asked by 6 years ago
Edited 6 years ago

So I'm trying to make a shop, but I have a problem. To equip a weapon, you first need to buy it. This works perfectly. When you own the weapon (it's basically just a boolean value saying whether or not you have bought it) you can click on a textbutton to equip it. But I have to make sure you don't already have a weapon equipped.

For example: you try to equip a primary weapon, but you already have one in your backpack. What should happen is that the one you already have equipped disappears and gets replaced by the new one. Your secondary weapon / other items in backpack should NOT be deleted.

To do this I came up with this system:

This script copies the weapon into a folder into your backpack. (They're called primary and secondary)

localscript in the Equip button

Do not look at any of the variables, this works.

script.Parent.MouseButton1Click:connect(function()
    if HasWeapon.Value then
        local CloneGun = Gun:Clone()
        if Class.Value == "Primary" then
            Primary:ClearAllChildren()
            CloneGun.Parent = game.Players.LocalPlayer.Backpack.Primary
        else
            if Class.Value == "Secondary" then
                Secondary:ClearAllChildren()
                CloneGun.Parent = game.Players.LocalPlayer.Backpack.Secondary
            end
        end     
        print("You have Equipped your weapon")
    else
        print("You do not own this weapon")
    end
end)

But you can't equip tools in a folder in your backpack! So when you add an item to one of these folders it should be copied into your main backpack, and deleted whenever another item appears in the folder.

localscript in backpack This is to copy the tool into your backpack so you can use it, and delete it when an item gets added to the folder.

--local variables
local Primary = script.Parent.Primary --These are the two folders
local Secondary = script.Parent.Secondary --These are the two folders


Primary.ChildAdded:connect(function(ObjectP)
    local CloneObj = ObjectP:Clone()
    CloneObj.Parent = script.Parent
    ObjectP:destroy()
    Primary.ChildAdded:connect(function(Object2)
        CloneObj:destroy()
    end)
end)

Secondary.ChildAdded:connect(function(ObjectS)
    local CloneObj = ObjectS:Clone()
    CloneObj.Parent = script.Parent
    Secondary.ChildAdded:connect(function(Object2)
        CloneObj:destroy()
    end)
end)

Now what happens is the following: when you click equip, the tool appears in the folder and then in your backpack. But sometimes when I try to actually use the weapon, the weapon doesn't work, the tool spawns in the middle of the map, and my hands look like they are trying to shoot with air. I get launched into the air a bit and the fall down, and I can go straight through walls and parts!

Sometimes the tool works just fine. It's about a 50/50 chance the tool works.

The weirdest part is: when I copy the broken version of the tool, and then put it back into the backpack (copy tool, stop testing, copy into starterpack and then test again) it suddenly works! The broken version seems identical to the working one.

Anyone got any idea how to fix this problem? All help is appreciated. :)

Answer this question