Advertisement
HowToRoblox

InventoryGuiHandler

Jan 8th, 2023
2,112
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.57 KB | None | 1 0
  1. game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
  2.  
  3. local keys = {
  4.     [1] = Enum.KeyCode.One;
  5.     [2] = Enum.KeyCode.Two;
  6.     [3] = Enum.KeyCode.Three;
  7.     [4] = Enum.KeyCode.Four;
  8.     [5] = Enum.KeyCode.Five;
  9.     [6] = Enum.KeyCode.Six;
  10.     [7] = Enum.KeyCode.Seven;
  11.     [8] = Enum.KeyCode.Eight;
  12.     [9] = Enum.KeyCode.Nine;
  13. }
  14.  
  15. local uis = game:GetService("UserInputService")
  16.  
  17. local rs = game:GetService("ReplicatedStorage"):WaitForChild("InventoryReplicatedStorage")
  18. local remotes = rs:WaitForChild("RemoteEvents")
  19.  
  20. local client = game.Players.LocalPlayer
  21.  
  22. local hotbar = client:WaitForChild("Hotbar")
  23. local inventory = client:WaitForChild("Inventory")
  24. local equipped = client:WaitForChild("Equipped")
  25.  
  26. local hotbarGui = script.Parent:WaitForChild("Hotbar"):WaitForChild("SlotsContainer")
  27. local invGui = script.Parent:WaitForChild("Inventory")
  28. invGui.Visible = false
  29. local openInv = script.Parent:WaitForChild("OpenInventory")
  30.  
  31.  
  32. function updateHotbar()
  33.  
  34.     for slotNum, slotVal in pairs(hotbar:GetChildren()) do
  35.  
  36.         local guiSlot = hotbarGui[slotNum]
  37.  
  38.         if slotVal.Value then
  39.             guiSlot.ItemName.Visible = true
  40.             guiSlot.ItemName.Text = slotVal.Value.Name
  41.  
  42.             if string.len(slotVal.Value.TextureId) > 0 then
  43.                 guiSlot.ItemViewer.Visible = false
  44.                 guiSlot.ItemImage.Visible = true
  45.                
  46.                 guiSlot.ItemImage.Image = slotVal.Value.TextureId
  47.             else
  48.                 guiSlot.ItemViewer.Visible = true
  49.                 guiSlot.ItemImage.Visible = false
  50.                
  51.                 guiSlot.ItemViewer:ClearAllChildren()
  52.  
  53.                 local vpfModel = Instance.new("Model")
  54.  
  55.                 local copiedTool = slotVal.Value:Clone()
  56.                 for __, desc in pairs(copiedTool:GetDescendants()) do
  57.                     if desc:IsA("LocalScript") or desc:IsA("Script") then
  58.                         desc:Destroy()
  59.                     elseif desc:IsA("BasePart") then
  60.                         desc.Parent = vpfModel
  61.                     end
  62.                 end
  63.                 copiedTool:Destroy()
  64.  
  65.                 local cf, size = vpfModel:GetBoundingBox()
  66.  
  67.                 local primaryPart = Instance.new("Part")
  68.                 primaryPart.Transparency = 1
  69.                 primaryPart.CFrame = cf
  70.                 primaryPart.Parent = vpfModel
  71.  
  72.                 vpfModel.PrimaryPart = primaryPart
  73.  
  74.                 vpfModel:PivotTo(CFrame.new(Vector3.new(0, 0, 0), Vector3.new(0, math.rad(90), math.rad(90))))
  75.  
  76.                 local itemPivot = vpfModel:GetPivot()
  77.                 local itemSize = vpfModel:GetExtentsSize()
  78.  
  79.                 local vpfCam = Instance.new("Camera")
  80.                 vpfCam.CFrame = CFrame.new(itemPivot.Position + (Vector3.one * itemSize * 0.5), itemPivot.Position)
  81.                 guiSlot.ItemViewer.CurrentCamera = vpfCam
  82.  
  83.                 vpfCam.Parent = guiSlot.ItemViewer
  84.                 vpfModel.Parent = guiSlot.ItemViewer
  85.             end
  86.            
  87.         else
  88.             guiSlot.ItemName.Visible = false
  89.             guiSlot.ItemImage.Visible = false
  90.             guiSlot.ItemViewer.Visible = false
  91.         end
  92.     end
  93. end
  94.  
  95. function updateInventory()
  96.    
  97.     for _, invItem in pairs(invGui.InventoryScroller:GetChildren()) do
  98.         if invItem:IsA(script.InventoryItem.ClassName) then
  99.             invItem:Destroy()
  100.         end
  101.     end
  102.    
  103.     for _, toolVal in pairs(inventory:GetChildren()) do
  104.        
  105.         local tool = toolVal.Value
  106.        
  107.         local newItem = script.InventoryItem:Clone()
  108.         newItem.ItemName.Text = tool.Name
  109.        
  110.         if string.len(tool.TextureId) > 0 then
  111.             newItem.ItemViewer.Visible = false
  112.             newItem.ItemImage.Image = tool.TextureId
  113.         else
  114.             newItem.ItemImage.Visible = false
  115.            
  116.             newItem.ItemViewer:ClearAllChildren()
  117.  
  118.             local vpfModel = Instance.new("Model")
  119.  
  120.             local copiedTool = tool:Clone()
  121.             for __, desc in pairs(copiedTool:GetDescendants()) do
  122.                 if desc:IsA("LocalScript") or desc:IsA("Script") then
  123.                     desc:Destroy()
  124.                 elseif desc:IsA("BasePart") then
  125.                     desc.Parent = vpfModel
  126.                 end
  127.             end
  128.             copiedTool:Destroy()
  129.  
  130.             local cf, size = vpfModel:GetBoundingBox()
  131.  
  132.             local primaryPart = Instance.new("Part")
  133.             primaryPart.Transparency = 1
  134.             primaryPart.CFrame = cf
  135.             primaryPart.Parent = vpfModel
  136.  
  137.             vpfModel.PrimaryPart = primaryPart
  138.  
  139.             vpfModel:PivotTo(CFrame.new(Vector3.new(0, 0, 0), Vector3.new(0, math.rad(90), math.rad(90))))
  140.  
  141.             local itemPivot = vpfModel:GetPivot()
  142.             local itemSize = vpfModel:GetExtentsSize()
  143.  
  144.             local vpfCam = Instance.new("Camera")
  145.             vpfCam.CFrame = CFrame.new(itemPivot.Position + (Vector3.one * itemSize * 0.5), itemPivot.Position)
  146.             newItem.ItemViewer.CurrentCamera = vpfCam
  147.            
  148.             vpfCam.Parent = newItem.ItemViewer
  149.             vpfModel.Parent = newItem.ItemViewer
  150.         end
  151.        
  152.         newItem.MouseButton1Click:Connect(function()
  153.             remotes.ToHotbar:FireServer(tool)
  154.         end)
  155.        
  156.         newItem.MouseButton2Click:Connect(function()
  157.             remotes.Drop:FireServer(tool)
  158.         end)
  159.        
  160.         newItem.Parent = invGui.InventoryScroller
  161.     end
  162. end
  163.  
  164. function findSelectedSlot()
  165.    
  166.     for slotNum, slotVal in pairs(hotbar:GetChildren()) do
  167.        
  168.         local guiSlot = hotbarGui[slotNum]
  169.        
  170.         if slotVal.Value and slotVal.Value == equipped.Value then
  171.             guiSlot.ImageColor3 = Color3.fromRGB(255, 255, 255)
  172.         else
  173.             guiSlot.ImageColor3 = Color3.fromRGB(218, 218, 218)
  174.         end
  175.     end
  176. end
  177.  
  178.  
  179. for slotNum, slotVal in pairs(hotbar:GetChildren()) do
  180.    
  181.     local newSlot = script:WaitForChild("HotbarItem"):Clone()
  182.     newSlot.Name = slotNum
  183.    
  184.     if keys[slotNum].Value >= 48 and keys[slotNum].Value <= 57 then
  185.         newSlot.NumberKey.Text = keys[slotNum].Value - 48
  186.     else
  187.         newSlot.NumberKey.Text = string.split(tostring(keys[slotNum]), ".")[3]
  188.     end
  189.    
  190.     if slotVal.Value then
  191.         newSlot.ItemName.Text = slotVal.Value.Name
  192.        
  193.         if string.len(slotVal.Value.TextureId) > 0 then
  194.             newSlot.ItemViewer.Visible = false
  195.             newSlot.ItemImage.Image = slotVal.Value.TextureId
  196.         else
  197.             newSlot.ItemImage.Visible = false
  198.            
  199.             newSlot.ItemViewer:ClearAllChildren()
  200.  
  201.             local vpfModel = Instance.new("Model")
  202.  
  203.             local copiedTool = slotVal.Value:Clone()
  204.             for __, desc in pairs(copiedTool:GetDescendants()) do
  205.                 if desc:IsA("LocalScript") or desc:IsA("Script") then
  206.                     desc:Destroy()
  207.                 elseif desc:IsA("BasePart") then
  208.                     desc.Parent = vpfModel
  209.                 end
  210.             end
  211.             copiedTool:Destroy()
  212.            
  213.             local cf, size = vpfModel:GetBoundingBox()
  214.            
  215.             local primaryPart = Instance.new("Part")
  216.             primaryPart.Transparency = 1
  217.             primaryPart.CFrame = cf
  218.             primaryPart.Parent = vpfModel
  219.            
  220.             vpfModel.PrimaryPart = primaryPart
  221.            
  222.             vpfModel:PivotTo(CFrame.new(Vector3.new(0, 0, 0), Vector3.new(0, math.rad(90), math.rad(90))))
  223.            
  224.             local itemPivot = vpfModel:GetPivot()
  225.             local itemSize = vpfModel:GetExtentsSize()
  226.            
  227.             local vpfCam = Instance.new("Camera")
  228.             vpfCam.CFrame = CFrame.new(itemPivot.Position + (Vector3.one * itemSize * 0.5), itemPivot.Position)
  229.             newSlot.ItemViewer.CurrentCamera = vpfCam
  230.            
  231.             vpfCam.Parent = newSlot.ItemViewer
  232.            
  233.             vpfModel.Parent = newSlot.ItemViewer
  234.         end
  235.        
  236.     else
  237.         newSlot.ItemName.Visible = false
  238.         newSlot.ItemImage.Visible = false
  239.         newSlot.ItemViewer.Visible = false
  240.     end
  241.    
  242.     newSlot.MouseButton1Click:Connect(function()
  243.         local tool = slotVal.Value
  244.         remotes.Equip:FireServer(tool)
  245.     end)
  246.    
  247.     newSlot.MouseButton2Click:Connect(function()
  248.         local tool = slotVal.Value
  249.         remotes.ToInventory:FireServer(tool)
  250.     end)
  251.    
  252.     newSlot.Parent = hotbarGui
  253.    
  254.     slotVal:GetPropertyChangedSignal("Value"):Connect(updateHotbar)
  255. end
  256.  
  257.  
  258. updateInventory()
  259. inventory.ChildAdded:Connect(updateInventory)
  260. inventory.ChildRemoved:Connect(updateInventory)
  261.  
  262.  
  263. findSelectedSlot()
  264. equipped:GetPropertyChangedSignal("Value"):Connect(findSelectedSlot)
  265.  
  266.  
  267. uis.InputBegan:Connect(function(inp, p)
  268.    
  269.     if p then return end
  270.    
  271.     if table.find(keys, inp.KeyCode) then
  272.        
  273.         local slotVal = hotbar[table.find(keys, inp.KeyCode)]
  274.         local tool = slotVal.Value
  275.         remotes.Equip:FireServer(tool)
  276.     end
  277. end)
  278.  
  279.  
  280. openInv.MouseButton1Click:Connect(function()
  281.     invGui.Visible = not invGui.Visible
  282. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement