Hi I Have This Script That Is Supposed To Create A Custom Inventory That Can Hold 10 items. Obviously It Wont Work. Can Someone Help (Script Below)
local player = game.Players.LocalPlayer repeat wait() until player.Character local character = player.Character local inventory = player.Backpack:GetChildren() local amount = 0 player.Backpack.ChildAdded:connect(function(child) if child:IsA("Tool") then amount = amount + 1 if amount == 11 then wait(.1) child:Destroy() end end end) player.Backpack.ChildRemoved:connect(function(removed) if removed:IsA("Tool") and amount > 11 then amount = amount - 1 print(amount) end end)
Tip For The Future: You Need Not Capitalize Every Word Like This, Which Is In Fact Wrong To Do.
That aside, let's help you through the script, because you've clearly put effort to it before presenting it. Before you ask, that's a good thing.
Along with the character you will need to wait for the player too, unless you manually (via another script) give the script to the player.
To get the amount of items in a table, you can use # and cut yourself a variable (in this case, "amount"). For instance, #player.Backpack:GetChildren()
will get the amount of items the player is carrying, supposing player is game.Players.LocalPlayer.
In cases where you are connecting to an event where a child/player is removed, it's ChildRemoving and PlayerRemoving accordingly. Instances don't exactly remove the instant they are called to, so the Roblox devs decided to name the event as such.
When you set a variable, the value doesn't update unless you set it again. So you'd have to set inventory every time it was required to get the accurate table of items.
I left the character variable in just in case you'll use it later.
repeat wait() until game.Players.LocalPlayer local player = game.Players.LocalPlayer repeat wait() until player.Character local character = player.Character --Inventory, whose items weren't used at all was removed. --Amount is used for the number of items in the backpack in each event. --Variables only update when you set them, with the exclusion of setting them --to an existing object. player.Backpack.ChildAdded:connect(function(child) local amount = #player.Backpack:GetChildren() if child:IsA("Tool") then if amount == 11 then wait(.1) child:Destroy() end end end) player.Backpack.ChildRemoving:connect(function(removed) local amount = #player.Backpack:GetChildren() if removed:IsA("Tool") and amount > 10 then -- Not sure what you want to do here print(amount) end end)