Advertisement
Sungmingamerpro13

StackHotBar System

Apr 12th, 2024
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 5.92 KB | None | 0 0
  1. -- [ SETTINGS ] --
  2.  
  3. local backgroundColor = Color3.fromRGB(0, 132, 255) -- The color of each circle
  4. local maxOnHotbar = 10 -- How many tools can be displayed. Setting this to more than 10 reverts to 10
  5.  
  6. -- [ END SETTINGS ] --
  7.  
  8. -- Don't edit if you don't know what you're doing --
  9.  
  10. local Players = game:GetService("Players")
  11. local StarterGui = game:GetService("StarterGui")
  12. local UserInputService = game:GetService("UserInputService")
  13. local TweenService = game:GetService("TweenService")
  14. local player = Players.LocalPlayer
  15. local Backpack = player.Backpack
  16. local Character = player.Character
  17. local Frame = script.Parent.Frame
  18. local Template = Frame.Contents.objTemplate
  19.  
  20. local COLOR_DARKER = 0.65
  21. local KEY_DICTIONARY = { Zero = 10, One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9 }
  22. local EQUIP_TWEENINFO = TweenInfo.new(0.25, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
  23. local items = {}
  24. local tweens = {}
  25. local stackedItems = {}
  26.  
  27. local function handleStackingUI(toolCircle, tool)
  28.     if tool:WaitForChild("stacks").Value then
  29.         tool.stacks:GetPropertyChangedSignal('Value'):Connect(function()
  30.             toolCircle.Stacks.Text = tool.stacks.Value
  31.         end)
  32.         toolCircle.Stacks.Text = tool.stacks.Value
  33.     end
  34. end
  35.  
  36. local function handleEquip(tool)
  37.     if tool.Parent == nil or tool.Parent == Backpack then
  38.         Character.Humanoid:EquipTool(tool)
  39.     elseif tool.Parent == Character then
  40.         Character.Humanoid:UnequipTools()
  41.     end
  42. end
  43.  
  44.  
  45. local function checkHotbar(removeIndex)
  46.     if removeIndex then
  47.         for _, toolCircle in pairs(Frame.Contents:GetChildren()) do
  48.             if toolCircle:IsA("GuiObject") then
  49.                 local circleName = tonumber(toolCircle.Name)
  50.                 if circleName and circleName >= removeIndex then
  51.                     local newIndex = (circleName == 11) and 0 or circleName - 1
  52.                     local isVisible = (maxOnHotbar == 10) and (newIndex <= 9 and true or false) or ((newIndex ~= 0 and newIndex <= maxOnHotbar) and true or false)
  53.  
  54.                     toolCircle.LayoutOrder -= 1
  55.                     toolCircle.Name = tostring(newIndex)
  56.                     toolCircle.Visible = isVisible
  57.                     toolCircle.Index.Text = tostring(newIndex)
  58.                 end
  59.             end
  60.         end
  61.     end
  62.  
  63.     local additionalIndex = Frame.AdditionalIndex
  64.     additionalIndex.Size = UDim2.new(0, Frame.Contents.UIListLayout.AbsoluteContentSize.X + Frame.Contents.AbsoluteSize.Y + 15, 0, 25)
  65.     additionalIndex.Text = "+" .. (#items - maxOnHotbar)
  66.     additionalIndex.Visible = (#items > maxOnHotbar)
  67.  
  68.     for index, tool in ipairs(items) do
  69.         local toolCircle = Frame.Contents:FindFirstChild(index)
  70.         local isEquipped = (tool.Parent == Character)
  71.  
  72.         if not toolCircle then
  73.             return
  74.         end
  75.  
  76.         if tweens[toolCircle] then
  77.             tweens[toolCircle]:Cancel()
  78.             tweens[toolCircle] = nil
  79.         end
  80.  
  81.         -- Adjust UIStroke Transparency based on whether the tool is equipped or not
  82.         toolCircle.Background.Out.UIStroke.Transparency = isEquipped and 0 or 1
  83.         -- Adjust BackgroundTransparency of the circle Frame based on the UIStroke Transparency
  84.         toolCircle.Background.Out.BackgroundTransparency = isEquipped and 0.7 or 0.7
  85.        
  86.         handleStackingUI(toolCircle, tool)
  87.     end
  88. end
  89.  
  90.  
  91.  
  92. local function create(tool)
  93.    
  94.     local nextIndex = (#items == 10) and 0 or #items
  95.     local isVisible = (maxOnHotbar == 10) and (nextIndex <= 9 and true or false) or ((nextIndex ~= 0 and nextIndex <= maxOnHotbar) and true or false)
  96.  
  97.     local toolCircle = Template:Clone()
  98.    
  99.     toolCircle.LayoutOrder = #items
  100.     toolCircle.Name = #items
  101.     toolCircle.Size = UDim2.new(0.071, 0,0.739, 0)
  102.     toolCircle.Visible = isVisible
  103.     toolCircle.Image.Image = tool.TextureId
  104.     toolCircle.Index.Text = nextIndex
  105.     toolCircle.Stacks.Text = "1"
  106.     toolCircle.FixedName.Text = tool.TextureId == "" and tool.Name or ""
  107.     toolCircle.Parent = Frame.Contents
  108.  
  109.     if tool.Name == "Bat" or tool.Name == "WoodenSword" or tool.Name == "Dark Sword" or tool.Name == "Demon Sword" or tool.Name == "Rainbow Sword" then
  110.         toolCircle.Stacks.Text = ""
  111.     elseif tool.Name == "ClassicSword" or tool.Name == "Crowbar" or tool.Name == "Metal Bat" or tool.Name == "DeluxeBat" or tool.Name == "PlusBat" then
  112.         toolCircle.Stacks.Text = ""
  113.     elseif tool.Name == "GoldBat" or tool.Name == "RobuxBat" or tool.Name == "Spiked Club" or tool.Name == "Light Saber" or tool.Name == "Flashlight" then
  114.         toolCircle.Stacks.Text = ""
  115.     elseif tool.Name == "Plank" then
  116.         toolCircle.Stacks.Text = ""
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement