Advertisement
Sungmingamerpro13

Backpack HotBar (Definitive)

Nov 13th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 5.47 KB | None | 0 0
  1. -- SungExetior13RBLX --
  2.  
  3. -- [ SETTINGS ] --
  4.  
  5. local backgroundColor = Color3.fromRGB(0, 132, 255) -- The color of each circle
  6. local maxOnHotbar = 10 -- How many tools can be displayed. Setting this to more than 10 reverts to 10
  7.  
  8. -- [ END SETTINGS ] --
  9.  
  10. -- Don't edit if you don't know what you're doing --
  11.  
  12. local Players = game:GetService("Players")
  13. local StarterGui = game:GetService("StarterGui")
  14. local UserInputService = game:GetService("UserInputService")
  15. local TweenService = game:GetService("TweenService")
  16. local player = Players.LocalPlayer
  17. local Backpack = player.Backpack
  18. local Character = player.Character
  19. local Frame = script.Parent.Frame
  20. local Template = Frame.Contents.objTemplate
  21.  
  22. local COLOR_DARKER = 0.65
  23. local KEY_DICTIONARY = { Zero = 10, One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9 }
  24. local EQUIP_TWEENINFO = TweenInfo.new(0.25, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
  25. local items = {}
  26. local tweens = {}
  27. local stackedItems = {}
  28.  
  29. local function handleStackingUI(toolCircle, tool)
  30.     if tool:WaitForChild("stacks").Value then
  31.         tool.stacks:GetPropertyChangedSignal('Value'):Connect(function()
  32.             toolCircle.Stacks.Text = tool.stacks.Value
  33.            
  34.             if tool.stacks.Value == 1 then
  35.                 toolCircle.Stacks.Visible = false
  36.             end
  37.         end)
  38.         toolCircle.Stacks.Text = tool.stacks.Value
  39.        
  40.     end
  41. end
  42.  
  43. local function handleEquip(tool)
  44.     if tool.Parent == nil or tool.Parent == Backpack then
  45.         Character.Humanoid:EquipTool(tool)
  46.     elseif tool.Parent == Character then
  47.         Character.Humanoid:UnequipTools()
  48.     end
  49. end
  50.  
  51.  
  52. local function checkHotbar(removeIndex)
  53.     if removeIndex then
  54.         for _, toolCircle in pairs(Frame.Contents:GetChildren()) do
  55.             if toolCircle:IsA("GuiObject") then
  56.                 local circleName = tonumber(toolCircle.Name)
  57.                 if circleName and circleName >= removeIndex then
  58.                     local newIndex = (circleName == 11) and 0 or circleName - 1
  59.                     local isVisible = (maxOnHotbar == 10) and (newIndex <= 9 and true or false) or ((newIndex ~= 0 and newIndex <= maxOnHotbar) and true or false)
  60.  
  61.                     toolCircle.LayoutOrder -= 1
  62.                     toolCircle.Name = tostring(newIndex)
  63.                     toolCircle.Visible = isVisible
  64.                     toolCircle.Index.Text = tostring(newIndex)
  65.                 end
  66.             end
  67.         end
  68.     end
  69.  
  70.     local additionalIndex = Frame.AdditionalIndex
  71.     additionalIndex.Size = UDim2.new(0, Frame.Contents.UIListLayout.AbsoluteContentSize.X + Frame.Contents.AbsoluteSize.Y + 15, 0, 25)
  72.     additionalIndex.Text = "+1" .. (#items - maxOnHotbar)
  73.     additionalIndex.Visible = (#items > maxOnHotbar)
  74.  
  75.     for index, tool in ipairs(items) do
  76.         local toolCircle = Frame.Contents:FindFirstChild(index)
  77.         local isEquipped = (tool.Parent == Character)
  78.  
  79.         if not toolCircle then
  80.             return
  81.         end
  82.  
  83.         if tweens[toolCircle] then
  84.             tweens[toolCircle]:Cancel()
  85.             tweens[toolCircle] = nil
  86.         end
  87.  
  88.         -- Adjust UIStroke Transparency based on whether the tool is equipped or not
  89.         toolCircle.Background.Out.UIStroke.Transparency = isEquipped and 0 or 1
  90.         -- Adjust BackgroundTransparency of the circle Frame based on the UIStroke Transparency
  91.         toolCircle.Background.Out.BackgroundTransparency = isEquipped and 0.7 or 0.7
  92.        
  93.         handleStackingUI(toolCircle, tool)
  94.     end
  95. end
  96.  
  97.  
  98.  
  99. local function create(tool)
  100.    
  101.     local nextIndex = (#items == 10) and 0 or #items
  102.     local isVisible = (maxOnHotbar == 10) and (nextIndex <= 9 and true or false) or ((nextIndex ~= 0 and nextIndex <= maxOnHotbar) and true or false)
  103.  
  104.     local toolCircle = Template:Clone()
  105.    
  106.     toolCircle.LayoutOrder = #items
  107.     toolCircle.Name = #items
  108.     toolCircle.Size = UDim2.new(1, -19,1, -19)
  109.     toolCircle.Visible = isVisible
  110.     toolCircle.Image.Image = tool.TextureId
  111.     toolCircle.Index.Text = nextIndex
  112.     toolCircle.FixedName.Text = tool.TextureId == "" and tool.Name or ""
  113.     toolCircle.Parent = Frame.Contents
  114.    
  115.     if tool.stacks.Value == 1 then
  116.         toolCircle.Stacks.Visible = false
  117.     end
  118.  
  119.     toolCircle.Image.MouseButton1Click:Connect(function()
  120.         local index = string.gmatch(toolCircle.Name,"%d+")
  121.         index = tonumber(index())
  122.         tool = items[index]
  123.         handleEquip(tool)
  124.     end)
  125.  
  126.     checkHotbar()
  127.    
  128. end
  129.  
  130. local function updateAdd(tool)
  131.     if not tool:IsA("Tool") then
  132.         return
  133.     end
  134.  
  135.     checkHotbar()
  136.  
  137.     if table.find(items, tool) then
  138.         return
  139.     end
  140.  
  141.     table.insert(items, tool)
  142.  
  143.     create(tool)
  144. end
  145.  
  146. local function updateRemove(tool)
  147.     if not tool:IsA("Tool") then
  148.         return
  149.     end
  150.  
  151.     if tool.Parent == Character or tool.Parent == Backpack then
  152.         return
  153.     end
  154.  
  155.     if table.find(items, tool) then
  156.         local index = table.find(items, tool)
  157.         local toolCircle = Frame.Contents:FindFirstChild(index)
  158.  
  159.         if toolCircle then
  160.             toolCircle:Destroy()
  161.         end
  162.  
  163.         table.remove(items, index)
  164.         checkHotbar(index)
  165.     end
  166. end
  167.  
  168. while true do
  169.     local success, err = pcall(function()
  170.         StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
  171.     end)
  172.     if success then
  173.         break
  174.     end
  175.     wait()
  176. end
  177.  
  178. if maxOnHotbar > 10 then
  179.     maxOnHotbar = 10
  180. end
  181.  
  182. Template.Visible = true
  183. Template.Parent = nil
  184.  
  185. for _, tool in pairs(Backpack:GetChildren()) do
  186.     updateAdd(tool)
  187. end
  188. Backpack.ChildAdded:Connect(updateAdd)
  189. Backpack.ChildRemoved:Connect(updateRemove)
  190.  
  191. Character.ChildAdded:Connect(updateAdd)
  192. Character.ChildRemoved:Connect(updateRemove)
  193.  
  194. UserInputService.InputBegan:Connect(function(input, gameProcessed)
  195.     if gameProcessed then
  196.         return
  197.     end
  198.  
  199.     if KEY_DICTIONARY[input.KeyCode.Name] then
  200.         local index = KEY_DICTIONARY[input.KeyCode.Name]
  201.  
  202.         if items[index] then
  203.             handleEquip(items[index])
  204.         end
  205.     end
  206. end)
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement