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

My Inventory System adds multiple items and not just one, why is that?

Asked by 9 years ago

Basically, I have got the Inventory System to work pretty darn well. When the player presses E on their keyboard the item is then removed from the world and put into their inventory. The problem is that when the item is picked up, multiple quantitys of that time is added and not just one. It should add 1 item and not like 6.

local player = game:GetService("Players").LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
local inv_gui = script.Parent.Inventory
local inv = player:WaitForChild("Inventory")

while wait(0.1) do
    for _,item in pairs(workspace["WinterSurvival"].DroppedItems:GetChildren()) do
        if (item:IsA("Model")) then
            local itemID
            function findID(par)
                for _,kid in ipairs(par:GetChildren()) do
                    if (kid:IsA("Model")) then
                        itemID = kid.ItemID.Value
                    else
                        findID(kid)
                    end
                end
            end
            findID(game:GetService("ReplicatedStorage").Items)
            local label = script.Parent.PressE
            if (char:FindFirstChild("Torso").Position - item.Handle.Position).magnitude < 7 then
                label.Text = "Press [e] to pick up '"..item.Name.."'"
                label.Visible = true
                local debounce = true
                mouse.KeyDown:connect(function(key)
                    if (key:lower() == "e" and debounce) then
                        debounce = false
                        for _,slot in pairs(inv:GetChildren()) do
                            if (slot.ItemID.Value == itemID) then
                                slot.Qty.Value = slot.Qty.Value + 1
                                label.Visible = false
                                item:Destroy()
                                break
                            else
                                if (slot.ItemID.Value == 0) then
                                    slot.Qty.Value = 1
                                    inv_gui[slot.Name].Qty.Visible = true
                                    slot.ItemID.Value = itemID
                                    slot.Value = item.Img.Value
                                    label.Visible = false
                                    item:Destroy()
                                    break
                                end
                            end
                        end
                    end
                    debounce = true
                end)
            else
                label.Visible = false
            end
        else
            return
        end
    end
end
  • Thanks
0
Did you ever get this fixed? I'm looking through your post history and noticed this had no answers. adark 5487 — 9y

1 answer

Log in to vote
0
Answered by
ZeroBits 142
9 years ago

on line 27, change

and debounce

to

and debounce == true

this should fix things.

Why should this work?

well, when you say; "if value then" you're saying "if this value is not Nil then do this", now, false isn't nil, it's an actual value, so you have to say either "if value == true" or "if value ~= false" to get the desired result, depending on how strict you want to be with the script.

Ad

Answer this question