foolkiller204

Custom Inventory

Feb 13th, 2023 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.09 KB | Gaming | 0 0
  1. --PASTE INTO LOCAL SCRIPT!
  2. --LOCAL SCRIPT GOES INTO STARTER PLAYER SRIPTS!
  3.  
  4. --press I to open the inventory
  5. --right click on a item to remove it from the hotbar
  6. --left click a hotbar slot to toggle the item equip
  7. --[[left click a item in the inventory to select it
  8. then left click the hotbar slot you want to put it
  9. in]]
  10.  
  11. --[[If the tool you pick up dosnt have a texture ID
  12. the image will be set to black and yellow lines.]]
  13.  
  14. -- Removes default backpack
  15. game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
  16.  
  17. --services
  18. local players = game:GetService("Players")
  19. local UIS = game:GetService("UserInputService")
  20.  
  21. --var
  22. local player = players.LocalPlayer
  23. local character = player.Character or player.CharacterAdded:Wait()
  24. local humanoid = character:WaitForChild("Humanoid")
  25. local mouse = player:GetMouse()
  26. local screenGui = Instance.new("ScreenGui", player.PlayerGui)
  27. local backpack = {}
  28. local debounce = false
  29. local disable = false
  30. local backspace = false
  31. local selectedItem;
  32. local hotbar = {}
  33. local scrollingFrame = Instance.new("ScrollingFrame", screenGui)
  34.  
  35. --creates a list array to store hotbar data
  36. local function HotbarArray()
  37.     --[[creates nil data so that it can
  38.     later be replaced with item data
  39.     and maintaine its length]]
  40.     local hotbarSize = 10
  41.     for i = 1, hotbarSize do
  42.         if table.getn(hotbar) == 0 then
  43.             hotbar = {nil}
  44.         else
  45.             table.insert(hotbar, nil)
  46.         end
  47.     end
  48. end
  49. HotbarArray()
  50.  
  51. local function CreateInventory()
  52.     --sets the layout type
  53.     local gridLayout = Instance.new("UIGridLayout", scrollingFrame)
  54.     --property's
  55.     scrollingFrame.Size = UDim2.new(.792,0,.791,0)
  56.     scrollingFrame.Position = UDim2.new(.89,0,.85,0)
  57.     scrollingFrame.Visible = false
  58.     scrollingFrame.BackgroundColor3 = Color3.new(0, 0, 1)
  59.     scrollingFrame.BorderColor3 = Color3.new(0,0,0)
  60.     gridLayout.CellSize = UDim2.new(0,60,0,60)
  61.     scrollingFrame.AnchorPoint=Vector2.new(1,1)
  62. end
  63. CreateInventory()
  64.  
  65. local function EquipTool(item)
  66.     --debounce and wait is added to prevent bugs
  67.     debounce = true
  68.     humanoid:EquipTool(item)
  69.     wait()
  70.     debounce = false
  71. end
  72.  
  73. local function ToggleTool(index)
  74.     debounce = true
  75.     --[[checks to see if the item is already in use
  76.     to determine rather to unequip or equip]]
  77.     local itemInUse = false
  78.     --searches the character for the clicked item
  79.     for _, v in ipairs(character:GetChildren()) do
  80.         if v.ClassName == "Tool" and v == hotbar[index] then
  81.             humanoid:UnequipTools()
  82.             itemInUse = true
  83.         end
  84.     end
  85.     --Equips the tool
  86.     if not itemInUse then
  87.         humanoid:EquipTool(hotbar[index])
  88.     end
  89.     debounce = false
  90. end
  91.  
  92. local function RemoveFromHotbar(slot, index)
  93.     --clears hotbar slot
  94.     slot.Full.Value = nil
  95.     slot.Image = ""
  96.     table.remove(hotbar, index)
  97.     table.insert(hotbar, index, nil)
  98. end
  99.  
  100. local function RemoveFromBackpack(slot, item)
  101.     table.remove(backpack, table.find(backpack, item))
  102.     slot:Destroy()
  103. end
  104.  
  105. local function AddToBackpack(itemAdded)
  106.     --adds item to list array
  107.     if table.getn(backpack) == 0 then
  108.         --sets list value
  109.         backpack = {itemAdded}
  110.     else
  111.         --adds item to list
  112.         table.insert(backpack, itemAdded)
  113.     end
  114. --[[creates a slot for that item to be
  115. visible in the inventory]]
  116.     local slot = Instance.new("ImageButton", scrollingFrame)
  117.     slot.Name = "InventorySlot"
  118.     --if the items texture id is blank a default image is set
  119.     if itemAdded.TextureId == "" then
  120.         slot.Image = "http://www.roblox.com/asset/?id=12249128399"
  121.     else
  122.         slot.Image = itemAdded.TextureId
  123.     end
  124.     slot.BorderSizePixel = 3
  125.     slot.BorderColor = BrickColor.new("Black")
  126.     slot.Size = UDim2.new(0,60,0,60)
  127.     local val = Instance.new("ObjectValue", slot)
  128.     val.Name = "Full"
  129.     val.Value = itemAdded
  130.     --selects item in that slot that was clicked
  131.     slot.MouseButton1Click:Connect(function()
  132.         slot.BorderColor = BrickColor.new("Really red")
  133.         selectedItem = val.Value
  134.     end)
  135.     --unselects item in the slot
  136.     local function UnselectItem(Slot)
  137.         Slot.BorderColor = BrickColor.new("Black")
  138.         selectedItem = nil
  139.     end
  140.     --triggeres unselect
  141.     slot.MouseButton2Down:Connect(function()
  142.         UnselectItem(slot)
  143.     end)
  144.     --uhselects if inventory is closed
  145.     scrollingFrame.Changed:Connect(function()
  146.         if scrollingFrame.Visible == false then
  147.             UnselectItem(slot)
  148.         end
  149.     end)
  150. end
  151.  
  152. local function RefreshHotbar()
  153.     --local var
  154.     local index = 1
  155.     --scans through hotbar
  156.     for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
  157.         if slot.Name == "Slot" then
  158.             --if there is no item for that hotbar slot it gets cleared and the for loop ends
  159.             if hotbar[index] == "void" or hotbar[index] == nil then
  160.                 --clears hotbar slot
  161.                 slot.Full.Value = nil
  162.                 slot.Image = ""
  163.                 table.remove(hotbar, index)
  164.                 table.insert(hotbar, index, nil)
  165.                 index += 1
  166.             else
  167.                 --if there is an item then the slot updates
  168.                 local item = hotbar[index]
  169.                 slot.Full.Value = item
  170.                 --if the items texture id is blank a default image is set
  171.                 if item.TextureId == "" then
  172.                     slot.Image = "http://www.roblox.com/asset/?id=12249128399"
  173.                 else
  174.                     slot.Image = item.TextureId
  175.                 end
  176.                 index += 1
  177.             end
  178.             if slot.Full.Value ~= nil then
  179.                 local bool, errorMessage = pcall (function()
  180.                     if not character:FindFirstChild(slot.Full.Value.Name) and not player.Backpack:FindFirstChild(slot.Full.Value.Name) then
  181.                         --clears hotbar slot
  182.                         slot.Full.Value = nil
  183.                         slot.Image = ""
  184.                         table.remove(hotbar, index)
  185.                         table.insert(hotbar, index, nil)
  186.                         index += 1
  187.                     end
  188.                     local tool = slot.Full.Value
  189.                     if table.getn(tool:GetChildren()) == 0 then
  190.                         --clears hotbar slot
  191.                         slot.Full.Value = nil
  192.                         slot.Image = ""
  193.                         table.remove(hotbar, index)
  194.                         table.insert(hotbar, index, nil)
  195.                         index += 1
  196.                     end
  197.                 end)
  198.             end
  199.         end
  200.     end
  201. end
  202.  
  203. local function AddToHotbar(slot, item)
  204.     local index = slot.slotIndex.Value
  205.     hotbar[index] = item
  206.     RefreshHotbar()
  207. end
  208.  
  209. local function CreateHotbar()
  210.     --creates hotbar frame
  211.     local HotbarGui = Instance.new("Frame", screenGui)
  212.     HotbarGui.Transparency = 1
  213.     HotbarGui.Name = "Hotbar"
  214.     HotbarGui.Size = UDim2.new(0.673, 0,0.098, 0)
  215.     HotbarGui.Position = UDim2.new(.95, 0,1, 0)
  216.     HotbarGui.AnchorPoint = Vector2.new(1,1)
  217.     --adds grid layout for slots
  218.     local gridLayout = Instance.new("UIGridLayout", HotbarGui)
  219.     gridLayout.CellSize = UDim2.new(0,60,0,60)
  220.     --creates 10 hotbar slots
  221.     for i = 1, 10 do
  222.         --creates slot
  223.         local slot = Instance.new("ImageButton", HotbarGui)
  224.         slot.Name = "Slot"
  225.         slot.BorderSizePixel = 3
  226.         slot.BorderColor = BrickColor.new("Black")
  227.         --creates object value to keep track items in each slot
  228.         local val = Instance.new("ObjectValue", slot)
  229.         val.Name = "Full"
  230.         --creates a frame for the textlabel
  231.         local frame = Instance.new("Frame", slot)
  232.         frame.Size = UDim2.new(.3, 0, .3, 0)
  233.         frame.Position = UDim2.new(.7, 0, 0, 0)
  234.         Instance.new("UICorner", frame)
  235.         --text label displayes slots keybind number
  236.         local Text = Instance.new("TextLabel", frame)
  237.         --sets the text to i enless its the 10th slot then its set to 0
  238.         if i < 10 then
  239.             Text.Text = i
  240.         elseif i == 10 then
  241.             Text.Text = 0
  242.         end
  243.         --sets text properties
  244.         Text.BackgroundTransparency = 1
  245.         Text.TextScaled = true
  246.         Text.Size = UDim2.new(1,0,1,0)
  247.         --keeps track of the horbar slots index
  248.         local slotIndex = Instance.new("IntValue", slot)
  249.         slotIndex.Name = "slotIndex"
  250.         slotIndex.Value = i
  251.         slot.MouseButton1Click:Connect(function()
  252.             --if an item is selected it will move it to the slot
  253.             if selectedItem ~= nil then
  254.                 --if the slot it wants is full it will move the
  255.                 --item in that slot to the inventory
  256.                 if slot.Full.Value ~= nil then
  257.                     local item = slot.Full.Value
  258.                     AddToBackpack(item)
  259.                 end
  260.                 RemoveFromHotbar(slot, slotIndex.Value)
  261.                 --adds item and unselects it
  262.                 AddToHotbar(slot, selectedItem)
  263.                 --locates and deletes the slot
  264.                 for _, Slot in ipairs(screenGui.ScrollingFrame:GetChildren()) do
  265.                     if Slot.Name == "InventorySlot" then
  266.                         if Slot.Full.Value == selectedItem then
  267.                             RemoveFromBackpack(Slot, selectedItem)
  268.                         end
  269.                     end
  270.                 end
  271.                 selectedItem = nil
  272.             else
  273.                 --else it will just select the item in that slot
  274.                 ToggleTool(slotIndex.Value)
  275.             end
  276.         end)
  277.         --Puts tool away in inventory
  278.         slot.MouseButton2Click:Connect(function()
  279.             if slot.Full.Value ~= nil then
  280.                 local item = val.Value
  281.                 humanoid:UnequipTools()
  282.                 AddToBackpack(item)
  283.                 RemoveFromHotbar(slot, slotIndex.Value)
  284.             end
  285.         end)
  286.     end
  287. end
  288. CreateHotbar()
  289.  
  290. --Keybinds
  291. UIS.InputBegan:Connect(function(input)
  292.     local key = input.KeyCode
  293.  
  294.     --inventory keybind
  295.     if key==Enum.KeyCode.I then
  296.         local inv = screenGui.ScrollingFrame
  297.         --toggles visibility
  298.         if inv.Visible == true then inv.Visible = false else inv.Visible = true end
  299.     end
  300.  
  301.     --
  302.     if key == Enum.KeyCode.Backspace then
  303.         backspace = true
  304.     end
  305.  
  306.     --hotbar keybinds
  307.     if key==Enum.KeyCode.One then
  308.         if hotbar[1] then
  309.             ToggleTool(1)
  310.         end
  311.     end
  312.     if key==Enum.KeyCode.Two then
  313.         if hotbar[2] then
  314.             ToggleTool(2)
  315.         end
  316.     end
  317.     if key==Enum.KeyCode.Three then
  318.         if hotbar[3] then
  319.             ToggleTool(3)
  320.         end
  321.     end
  322.     if key==Enum.KeyCode.Four then
  323.         if hotbar[4] then
  324.             ToggleTool(4)
  325.         end
  326.     end
  327.     if key==Enum.KeyCode.Five then
  328.         if hotbar[5] then
  329.             ToggleTool(5)
  330.         end
  331.     end
  332.     if key==Enum.KeyCode.Six then
  333.         if hotbar[6] then
  334.             ToggleTool(6)
  335.         end
  336.     end
  337.     if key==Enum.KeyCode.Seven then
  338.         if hotbar[7] then
  339.             ToggleTool(7)
  340.         end
  341.     end
  342.     if key==Enum.KeyCode.Eight then
  343.         if hotbar[8] then
  344.             ToggleTool(8)
  345.         end
  346.     end
  347.     if key==Enum.KeyCode.Nine then
  348.         if hotbar[9] then
  349.             ToggleTool(9)
  350.         end
  351.     end
  352.     if key==Enum.KeyCode.Zero then
  353.         if hotbar[10] then
  354.             ToggleTool(10)
  355.         end
  356.     end
  357. end)
  358.  
  359. UIS.InputEnded:Connect(function(input)
  360.     local key = input.KeyCode
  361.  
  362.     if key == Enum.KeyCode.Backspace then
  363.         backspace = false
  364.         print(backspace)
  365.     end
  366. end)
  367.  
  368. --Attempts to add item to backpack
  369. player.Backpack.ChildAdded:Connect(function(itemAdded)
  370.     if table.getn(hotbar) >= 10 and debounce == false then
  371.         if not table.find(hotbar, itemAdded) and not table.find(backpack, itemAdded) then
  372.             AddToBackpack(itemAdded)
  373.         end
  374.     end
  375. end)
  376.  
  377. --picks up an item
  378. character.ChildAdded:Connect(function(itemAdded)
  379.     --checks if the player dosnt already own the item
  380.     if not table.find(backpack, itemAdded.Name) or not table.find(hotbar, itemAdded.Name) then
  381.         --searches through the hotbar
  382.         for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
  383.             if slot.Name == "Slot" then
  384.                 --checks conditions to prevent an error
  385.                 if itemAdded.ClassName == "Tool"  and debounce == false then
  386.                     --checks if hotbar is full
  387.                     if table.getn(hotbar) < 10 and debounce == false then
  388.                         if slot.Full.Value == nil then
  389.                             AddToHotbar(slot, itemAdded)
  390.                             break
  391.                         end
  392.                         --if hotbar is full adds to inventory
  393.                     elseif debounce == false then
  394.                         AddToBackpack(itemAdded)
  395.                         break
  396.                     end
  397.                 end
  398.             end
  399.         end
  400.     end
  401. end)
  402.  
  403. --adds starter gear
  404. for _, v in ipairs(player.Backpack:GetChildren()) do
  405.     --searches through the hotbar
  406.     for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
  407.         if slot.Name == "Slot" then
  408.             --checks conditions to prevent an error
  409.             if v.ClassName == "Tool"  and debounce == false then
  410.                 --checks if hotbar is full
  411.                 if table.getn(hotbar) < 10 and debounce == false then
  412.                     if slot.Full.Value == nil then
  413.                         AddToHotbar(slot, v)
  414.                         break
  415.                     end
  416.                     --if hotbar is full adds to inventory
  417.                 elseif debounce == false then
  418.                     AddToBackpack(v)
  419.                     break
  420.                 end
  421.             end
  422.         end
  423.     end
  424. end
  425.  
  426. --drops item
  427. character.ChildRemoved:Connect(function(itemRemoved)
  428.     --makes sure item wasnt just moved to backpack
  429.     if not table.find(backpack, itemRemoved) then
  430.         local index = table.find(hotbar, itemRemoved)
  431.         --searches through hotbar for  the right slot
  432.         for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
  433.             if  slot.Name == "Slot" then
  434.                 task.wait()
  435.                 --checks the slots index and if backspace is pressed
  436.                 if slot.slotIndex.Value == index and backspace == true then
  437.                     RemoveFromHotbar(slot, index)
  438.                 end
  439.             end
  440.         end
  441.     end
  442. end)
  443.  
  444. while true do
  445.     task.wait()
  446.     RefreshHotbar()
  447. end
Tags: #roblox
Add Comment
Please, Sign In to add comment