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

Limited inventory space + swapping gears not working?

Asked by 2 years ago

I want to limit my game to only one backpack slot.

The idea is that if a player is holding an item and wants to pick a different one up, the previous one would drop on the ground. The second one would be the one picked up.

The problem is that when the items get swapped, the one dropped on the ground can no longer be picked up anymore. Other than that, it's getting placed in the second slot instead of the first one.

The script I used:

local limit = 1


function updateTools()


    local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
    local char = script.Parent


    local tools = {}

    for i, child in pairs(char:GetChildren()) do
        if child:IsA("Tool") then table.insert(tools, child) end
    end

    for i, tool in pairs(backpack:GetChildren()) do
        table.insert(tools, tool)
    end 


    for i, tool in pairs(tools) do

        if i > limit then tool.Parent=workspace end
    end
end



local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")

backpack.DescendantAdded:Connect(updateTools)
backpack.DescendantRemoving:Connect(updateTools)

workspace.DescendantAdded:Connect(updateTools)
workspace.DescendantRemoving:Connect(updateTools)

Answer this question