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

Cloning multiple items?

Asked by 10 years ago

So I wrote my script as an inventory pick up script. When I pick up ammo it adds the correct amount to the ammo value but when I pick up an object, it clones multiple items into my inventory.

char = script.Parent
torso = char:findFirstChild("Torso")
player = game.Players.LocalPlayer
control = player.Control
text = player.PlayerGui.Cam.BlackOut.Interact
resources = game.Lighting.Resources
mouse = player:GetMouse()
stats = player:findFirstChild("Player Stats")
inventory = stats.Inventory
maxdistance=10
grabbable = nil
select = player.PlayerGui.Cam.LookAt

function Distance(target)
    local p = target.CFrame.p
    local t = torso.CFrame.p
    local distance = (p-t).magnitude
    if distance <= maxdistance then
        return true
    else
        return false
    end
end

function UnLook()
    select.Adornee = nil
    text.Visible = false
end

function Grab(item,id,key)
    if key == "e" then
        local class = id.Value
        if class == "Ammo" then
            local value = inventory.Ammo:findFirstChild(item.Name)
            value.Value = value.Value + item.Amount.Value
            item:remove()
            grabbable:disconnect()
        end
        if class == "Pistol" then
            local pistol = resources.Weapons.Pistol:findFirstChild(item.Name):clone()
            pistol.Parent = inventory.Weapons
            item:remove()
            grabbable:disconnect()
        end
    end
end

function Look()
    if control.Value == true then
        local target = mouse.Target
        if target ~= nil then
            local id = target.Parent.Parent:FindFirstChild("Id")
            if id ~= nil then
                local item = id.Parent
                local grab = Distance(target)
                if grab == true then
                    text.Text = item.Name
                    text.Visible = true
                    select.Adornee = item
                    grabable = mouse.KeyDown:connect(function(key) Grab(item,id,key) end)   
                else
                    UnLook()
                end
            else
                UnLook()
            end
        else
            UnLook()
        end
    end
end

mouse.Move:connect(Look)

1 answer

Log in to vote
0
Answered by 10 years ago

How many items does it duplicate? When you pick up something, does it only give you 1 extra, or 25 extra?

This is vital information we need to know in order to answer.

0
That's just it, it's always a random amount. It could be 5 or it could be 20. soaprocks2 75 — 10y
Ad

Answer this question