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

[SOLVED] Why does my simple custom inventory not work properly?

Asked by
Velsity 218 Moderation Voter
4 years ago
Edited 4 years ago
CurrentSpots = {
    [1] = false;
    [2] = false;
    [3] = false;
    [4] = false;
    [5] = false;
}


for i, v in pairs(script.Parent:GetChildren()) do
    if v:IsA("ImageButton") then
        v.IMG.MouseButton1Click:Connect(function()
            Current = v
            Character.Humanoid:UnequipTools()
            for ind, ve in ipairs(CurrentSpots) do
                if tostring(ind) == tostring(Current) then
                    Character.Humanoid:EquipTool(Player.Backpack[tostring(ve)])
                end
            end
        end)
    end
end


function Remove(Tool)
    for i, v in ipairs(CurrentSpots) do
        print(v, Tool, Current,i)
        if tostring(v) == tostring(Tool) and tostring(Current) == tostring(i) then
            CurrentSpots[i] = false
            script.Parent[tostring(i)].IMG.Image = ""
        end
    end
end

Character.ChildRemoved:Connect(function(Child)
    if Child:IsA("Tool") and Child.Parent ~= Player.Backpack then
        Remove(Child)
    end
end)

Character.ChildAdded:Connect(function(Child)  --<<< Probably error origin
    if not Child:IsA("Tool") then return end
    local New = true
    for i, v in ipairs(CurrentSpots) do
        if CurrentSpots[i] == Child then
            New = false 
            break
        end     
    end
    if New == true then
        for i, v in ipairs(CurrentSpots) do
            if v == false then
                CurrentSpots[i] = Child
                Current = i
                script.Parent[tostring(i)].IMG.Image = Child.TextureId
                break
            end
        end
    end
end)


If you drop an item, then equip another item, it clones the item you dropped back into the GUI, while you do not have the item inside your backpack. So the script kind of fools itself into thinking that you still have the item, while you actually do not. It also gives this error as it tries to equip it: https://gyazo.com/821867ee88a2747011d932e23d8397b6

Heres a GIF of the error happening: https://gyazo.com/bb5f4031260364e73b324b0efe4c92f3

I've tried pretty much everything at this point. I seriously need help.

1 answer

Log in to vote
0
Answered by
Velsity 218 Moderation Voter
4 years ago
Edited 4 years ago

fixed, i made it so it equips the object thats stored within the spots instead of searching for the name inside of the backpack.

Ad

Answer this question