Welcome to the Forums!
Join thousands of other Roblox members with a free account.
Create Forum Account

Inventory limit help

  • This website was created by Roblox members to replace the official forums (because Roblox shut them down) Join us if you like Roblox!

Malteesuh

New Member
Reputation: 0 / 0 / 0
Feb 17, 2021
2
0
1
Roblox
Malteesuh
Hello, I am a new scripter as of last week. I was wondering if anyone would be willing to share some code that limits how many items somebody can have in their inventory. I would like to limit this but when a player goes over the specified amount of items they drop the item instead of removing it so other players can pick it up.

In short: some code that would drop an item when they go over the limit, Instead of removing it.

This would really help develop my game and give me experience in future ideas, thanks for any replies or help with this project.
 

Rukreep

🇪🇸 Top Spanish member 🇪🇸
VIP
Reputation: 1 / 1 / 0
Jun 7, 2018
3,472
6
599
113
Madrid, España
roblox.com
Roblox
Rukreep
Discord
Rukreep#4376
You can go to players and select the inventory then see its properties, now I don't know if that's how you do it actually no it's not but I'm pretty sure you make that script and put it in like ServiceScriptService or no, I don't know man I'm an amateur scripter (7 months)
you meant you're not an amateur scripter
 
  • Hmm
Reactions: bloxAmbassador

CallMeKY

Moderator
Ultra
Reputation: 6 / 0 / 0
Feb 25, 2018
1,496
42
609
113
Maine, USA
Roblox
CallMeKY
Discord
CallMeKY#5012
Hello, I am a new scripter as of last week. I was wondering if anyone would be willing to share some code that limits how many items somebody can have in their inventory. I would like to limit this but when a player goes over the specified amount of items they drop the item instead of removing it so other players can pick it up.

In short: some code that would drop an item when they go over the limit, Instead of removing it.

This would really help develop my game and give me experience in future ideas, thanks for any replies or help with this project.
I tried to explain what's going on as best I could with comments, but I'm not the greatest at explaining things that way. Let me know if there's anything going on that's confusing and I'll explain it more in-depth.

Tool Limit Drop Script:
local maxItemLimit = 3 -- number of tools allowed
local dropDistance = 10 -- how far in front of the player to drop the tool

-- just used to wait, since newly parented objects can throw an error if you try to change their parent too quick
local runService = game:GetService('RunService')

game:GetService('Players').PlayerAdded:Connect(function(player)
    
    -- when a character dies, a new backpack object is created, so we can't wait for it inside the scope of the first function
    player.CharacterAdded:Connect(function(character)
        
        -- wait for humanoid and backpack to exist
        local humanoid = character:WaitForChild('Humanoid')
        local backpack = player:WaitForChild('Backpack')
        
        -- equipped tools are parented to character, not backpack, so we need to account for both
        local function getTotalTools()
            local totalTools = 0
            totalTools += #backpack:GetChildren()
            for _, object in pairs(character:GetChildren()) do
                if object:IsA('Tool') then
                    totalTools += 1
                end
            end
            return totalTools
        end
        
        -- if new child is a tool and player is at tool capacity, drop new item 10 studs in front of them
        local function checkToolCount(child)
            if not child:IsA('Tool') then return end
            if getTotalTools(backpack, character) > maxItemLimit then
                runService.Heartbeat:Wait()
                child.Parent = workspace
                child:WaitForChild('Handle').Position = humanoid.RootPart.Position + (humanoid.RootPart.CFrame.LookVector * dropDistance)
            end
        end
        
        -- check tool count whenever a child is parented to backpack or character
        backpack.ChildAdded:Connect(function(child)
            checkToolCount(child)
        end)
        
        -- when a tool is picked up, it's automatically equipped (parented to character instead of backpack)
        character.ChildAdded:Connect(function(child)
            checkToolCount(child)
        end)
        
    end)
end)



-- generate tool and clone to each players' backpack every second just for the sake of the example
local tool = Instance.new('Tool')
local handle = Instance.new('Part', tool)
handle.Size = Vector3.new(1, 1, 1)
handle.Name = 'Handle'

while wait(1) do
    for _, player in pairs(game:GetService('Players'):GetPlayers()) do
        tool:Clone().Parent = player:WaitForChild('Backpack')
    end
end
 
  • Like
Reactions: matieo33
Solution

Malteesuh

New Member
Reputation: 0 / 0 / 0
Feb 17, 2021
2
0
1
Roblox
Malteesuh
Hello, I am a new scripter as of last week. I was wondering if anyone would be willing to share some code that limits how many items somebody can have in their inventory. I would like to limit this but when a player goes over the specified amount of items they drop the item instead of removing it so other players can pick it up.

In short: some code that would drop an item when they go over the limit, Instead of removing it.

This would really help develop my game and give me experience in future ideas, thanks for any replies or help with this project.
I tried to explain what's going on as best I could with comments, but I'm not the greatest at explaining things that way. Let me know if there's anything going on that's confusing and I'll explain it more in-depth.

Tool Limit Drop Script:
local maxItemLimit = 3 -- number of tools allowed
local dropDistance = 10 -- how far in front of the player to drop the tool

-- just used to wait, since newly parented objects can throw an error if you try to change their parent too quick
local runService = game:GetService('RunService')

game:GetService('Players').PlayerAdded:Connect(function(player)
   
    -- when a character dies, a new backpack object is created, so we can't wait for it inside the scope of the first function
    player.CharacterAdded:Connect(function(character)
       
        -- wait for humanoid and backpack to exist
        local humanoid = character:WaitForChild('Humanoid')
        local backpack = player:WaitForChild('Backpack')
       
        -- equipped tools are parented to character, not backpack, so we need to account for both
        local function getTotalTools()
            local totalTools = 0
            totalTools += #backpack:GetChildren()
            for _, object in pairs(character:GetChildren()) do
                if object:IsA('Tool') then
                    totalTools += 1
                end
            end
            return totalTools
        end
       
        -- if new child is a tool and player is at tool capacity, drop new item 10 studs in front of them
        local function checkToolCount(child)
            if not child:IsA('Tool') then return end
            if getTotalTools(backpack, character) > maxItemLimit then
                runService.Heartbeat:Wait()
                child.Parent = workspace
                child:WaitForChild('Handle').Position = humanoid.RootPart.Position + (humanoid.RootPart.CFrame.LookVector * dropDistance)
            end
        end
       
        -- check tool count whenever a child is parented to backpack or character
        backpack.ChildAdded:Connect(function(child)
            checkToolCount(child)
        end)
       
        -- when a tool is picked up, it's automatically equipped (parented to character instead of backpack)
        character.ChildAdded:Connect(function(child)
            checkToolCount(child)
        end)
       
    end)
end)



-- generate tool and clone to each players' backpack every second just for the sake of the example
local tool = Instance.new('Tool')
local handle = Instance.new('Part', tool)
handle.Size = Vector3.new(1, 1, 1)
handle.Name = 'Handle'

while wait(1) do
    for _, player in pairs(game:GetService('Players'):GetPlayers()) do
        tool:Clone().Parent = player:WaitForChild('Backpack')
    end
end
It seems as If your code is giving me the limit of items and looping dropping them on the floor.
Post automatically merged:

Hello, I am a new scripter as of last week. I was wondering if anyone would be willing to share some code that limits how many items somebody can have in their inventory. I would like to limit this but when a player goes over the specified amount of items they drop the item instead of removing it so other players can pick it up.

In short: some code that would drop an item when they go over the limit, Instead of removing it.

This would really help develop my game and give me experience in future ideas, thanks for any replies or help with this project.
I tried to explain what's going on as best I could with comments, but I'm not the greatest at explaining things that way. Let me know if there's anything going on that's confusing and I'll explain it more in-depth.

Tool Limit Drop Script:
local maxItemLimit = 3 -- number of tools allowed
local dropDistance = 10 -- how far in front of the player to drop the tool

-- just used to wait, since newly parented objects can throw an error if you try to change their parent too quick
local runService = game:GetService('RunService')

game:GetService('Players').PlayerAdded:Connect(function(player)
  
    -- when a character dies, a new backpack object is created, so we can't wait for it inside the scope of the first function
    player.CharacterAdded:Connect(function(character)
      
        -- wait for humanoid and backpack to exist
        local humanoid = character:WaitForChild('Humanoid')
        local backpack = player:WaitForChild('Backpack')
      
        -- equipped tools are parented to character, not backpack, so we need to account for both
        local function getTotalTools()
            local totalTools = 0
            totalTools += #backpack:GetChildren()
            for _, object in pairs(character:GetChildren()) do
                if object:IsA('Tool') then
                    totalTools += 1
                end
            end
            return totalTools
        end
      
        -- if new child is a tool and player is at tool capacity, drop new item 10 studs in front of them
        local function checkToolCount(child)
            if not child:IsA('Tool') then return end
            if getTotalTools(backpack, character) > maxItemLimit then
                runService.Heartbeat:Wait()
                child.Parent = workspace
                child:WaitForChild('Handle').Position = humanoid.RootPart.Position + (humanoid.RootPart.CFrame.LookVector * dropDistance)
            end
        end
      
        -- check tool count whenever a child is parented to backpack or character
        backpack.ChildAdded:Connect(function(child)
            checkToolCount(child)
        end)
      
        -- when a tool is picked up, it's automatically equipped (parented to character instead of backpack)
        character.ChildAdded:Connect(function(child)
            checkToolCount(child)
        end)
      
    end)
end)



-- generate tool and clone to each players' backpack every second just for the sake of the example
local tool = Instance.new('Tool')
local handle = Instance.new('Part', tool)
handle.Size = Vector3.new(1, 1, 1)
handle.Name = 'Handle'

while wait(1) do
    for _, player in pairs(game:GetService('Players'):GetPlayers()) do
        tool:Clone().Parent = player:WaitForChild('Backpack')
    end
end
It seems as If your code is giving me the limit of items and looping dropping them on the floor.
oh that was just the example, sorry :)