Advertisement
HowToRoblox

ArmorGuiHandler

Feb 25th, 2023
1,147
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.44 KB | None | 1 0
  1. --Gui components
  2. local btn = script.Parent:WaitForChild("OpenArmorImageButton")
  3. local frame = script.Parent:WaitForChild("ArmorInventoryFrame")
  4. frame.Visible = false
  5.  
  6. local invframe = frame:WaitForChild("InventoryFrame")
  7. local closebtn = frame:WaitForChild("CloseImageButton")
  8. local scrollingframe = invframe:WaitForChild("ArmorInventoryScrollingFrame")
  9. local statsframe = invframe:WaitForChild("ArmorStatsFrame")
  10. statsframe.Visible = false
  11. local charframe = frame:WaitForChild("CharacterFrame")
  12. local charvpf = charframe:WaitForChild("CharacterViewportFrame")
  13.  
  14. local wearingArmorButtons = {
  15.     Head  = charframe:WaitForChild("HeadImageButton");
  16.     Torso = charframe:WaitForChild("TorsoImageButton");
  17.     Hands = charframe:WaitForChild("HandsImageButton");
  18.     Legs  = charframe:WaitForChild("LegsImageButton");
  19.     Feet  = charframe:WaitForChild("FeetImageButton");
  20. }
  21.  
  22. local armorpieceTemplate = script:WaitForChild("ArmorPieceTextButton")
  23.  
  24. --RemoteEvents
  25. local remotes = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents")
  26. local equipRE = remotes:WaitForChild("EquipArmor")
  27. local removeRE = remotes:WaitForChild("RemoveArmor")
  28. local deleteRE = remotes:WaitForChild("DeleteArmor")
  29.  
  30. --Other variables
  31. local numslots = 30
  32.  
  33. local me = game.Players.LocalPlayer
  34. local myInv = me:WaitForChild("ArmorInventory")
  35. local myEquipped = me:WaitForChild("ArmorEquipped")
  36.  
  37. local mouse = me:GetMouse()
  38.  
  39. local armorpieces = game:GetService("ReplicatedStorage"):WaitForChild("ArmorPieces")
  40.  
  41.  
  42. --Stats frame
  43. local selectedSlot = nil
  44.  
  45. statsframe:WaitForChild("CloseTextButton").MouseButton1Click:Connect(function()
  46.     statsframe.Visible = false
  47.     selectedSlot = nil
  48. end)
  49.  
  50. statsframe:WaitForChild("EquipTextButton").MouseButton1Click:Connect(function()
  51.     statsframe.Visible = false
  52.    
  53.     for _, armorpiece in pairs(armorpieces:GetDescendants()) do
  54.         if armorpiece.Name == selectedSlot["ARMOR PIECE"].Value.Name and armorpiece.Parent.Parent == armorpieces then
  55.             equipRE:FireServer(armorpiece.Parent.Name, selectedSlot["ARMOR PIECE"].Value.Name)
  56.             break
  57.         end
  58.     end
  59. end)
  60.  
  61. statsframe:WaitForChild("DeleteTextButton").MouseButton1Click:Connect(function()
  62.     statsframe.Visible = false
  63.     deleteRE:FireServer(selectedSlot["ARMOR PIECE"].Value.Name)
  64. end)
  65.  
  66.  
  67. --Inventory set up
  68. for i = 1, numslots do
  69.     local newslot = armorpieceTemplate:Clone()
  70.     newslot.NameTextLabel.Visible = false
  71.     newslot.ModelViewportFrame.Visible = false
  72.     newslot.ModelImageLabel.Visible = false
  73.     newslot.Name = i
  74.     newslot.Parent = scrollingframe
  75.    
  76.     newslot.MouseButton1Click:Connect(function()
  77.        
  78.         if newslot.NameTextLabel.Visible == true then
  79.             selectedSlot = newslot
  80.            
  81.             statsframe.NameTextLabel.Text = newslot.NameTextLabel.Text
  82.            
  83.             local armor = newslot["ARMOR PIECE"].Value
  84.             local health = armor:WaitForChild("Stats").Health.Value
  85.             if health > 0 then
  86.                 health = "+" .. tostring(health)
  87.             end
  88.             local speed = armor:WaitForChild("Stats").Speed.Value
  89.             if speed > 0 then
  90.                 speed = "+" .. tostring(speed)
  91.             end
  92.             statsframe.HealthTextLabel.Text = "Health: " .. health
  93.             statsframe.SpeedTextLabel.Text = "Speed: " .. speed
  94.            
  95.             statsframe.Position = UDim2.new(0, mouse.X - statsframe.Parent.AbsolutePosition.X, 0, mouse.Y - statsframe.Parent.AbsolutePosition.Y)
  96.            
  97.             statsframe.Visible = true
  98.         end
  99.     end)
  100. end
  101.  
  102. function updateInv()
  103.     task.wait(0.1)
  104.     local inv = myInv:GetChildren()
  105.     table.sort(inv, function(a, b)
  106.         return (a.Stats.Health.Value + a.Stats.Speed.Value) > (b.Stats.Health.Value + b.Stats.Speed.Value)
  107.     end)
  108.    
  109.     for _, slot in pairs(scrollingframe:GetChildren()) do
  110.         if slot.ClassName == armorpieceTemplate.ClassName then
  111.            
  112.             if tonumber(slot.Name) > #inv then
  113.                 slot.NameTextLabel.Visible = false
  114.                 slot.ModelViewportFrame.Visible = false
  115.                 slot.ModelImageLabel.Visible = false
  116.                
  117.                 if slot:FindFirstChild("ARMOR PIECE") then
  118.                     slot["ARMOR PIECE"]:Destroy()
  119.                 end
  120.                
  121.             else
  122.                 slot.NameTextLabel.Text = inv[tonumber(slot.Name)].Name
  123.                
  124.                 if not inv[tonumber(slot.Name)]:WaitForChild("Stats"):FindFirstChild("Icon") then
  125.                     local armorModel = inv[tonumber(slot.Name)]:Clone()
  126.                     armorModel:PivotTo(CFrame.new())
  127.                     armorModel.Parent = slot.ModelViewportFrame
  128.                    
  129.                     local cf, size = armorModel:GetBoundingBox()
  130.                    
  131.                     local cc = Instance.new("Camera")
  132.                     cc.Name = "CurrentCamera"
  133.                     cc.CFrame = CFrame.new(size * 0.7 + Vector3.new(-1, 1, 0), cf.Position)
  134.                     cc.Parent = slot.ModelViewportFrame
  135.                     slot.ModelViewportFrame.CurrentCamera = cc
  136.                    
  137.                     slot.ModelImageLabel.Visible = false
  138.                     slot.ModelViewportFrame.Visible = true
  139.                    
  140.                 else
  141.                     slot.ModelImageLabel.Image = inv[tonumber(slot.Name)]:WaitForChild("Stats").Icon.Value
  142.                    
  143.                     slot.ModelViewportFrame.Visible = false
  144.                     slot.ModelImageLabel.Visible = true
  145.                 end
  146.                
  147.                 if slot:FindFirstChild("ARMOR PIECE") then
  148.                     slot["ARMOR PIECE"].Value = inv[tonumber(slot.Name)]
  149.                 else
  150.                     local armorpieceValue = Instance.new("ObjectValue")
  151.                     armorpieceValue.Name = "ARMOR PIECE"
  152.                     armorpieceValue.Value = inv[tonumber(slot.Name)]
  153.                     armorpieceValue.Parent = slot
  154.                 end
  155.                
  156.                 slot.NameTextLabel.Visible = true
  157.                 slot.Visible = true
  158.             end
  159.         end
  160.     end
  161.    
  162.     for _, equipped in pairs(myEquipped:GetChildren()) do
  163.        
  164.         if equipped.Value == nil then
  165.             wearingArmorButtons[equipped.Name].NameTextLabel.Visible = false
  166.             wearingArmorButtons[equipped.Name].ModelViewportFrame.Visible = false
  167.             wearingArmorButtons[equipped.Name].ModelImageLabel.Visible = false
  168.             wearingArmorButtons[equipped.Name].ModelViewportFrame:ClearAllChildren()
  169.            
  170.         else
  171.             wearingArmorButtons[equipped.Name].ModelViewportFrame:ClearAllChildren()
  172.            
  173.             for _, armorpiece in pairs(armorpieces:GetDescendants()) do
  174.                 if armorpiece.Name == equipped.Value.Name and armorpiece.Parent.Parent == armorpieces then
  175.                    
  176.                     if not armorpiece:WaitForChild("Stats"):FindFirstChild("Icon") then
  177.                         local armorModel = armorpiece:Clone()
  178.                         armorModel:PivotTo(CFrame.new())
  179.                         armorModel.Parent = wearingArmorButtons[equipped.Name].ModelViewportFrame
  180.  
  181.                         local cf, size = armorModel:GetBoundingBox()
  182.  
  183.                         local cc = Instance.new("Camera")
  184.                         cc.Name = "CurrentCamera"
  185.                         cc.CFrame = CFrame.new(size * 0.7 + Vector3.new(-1, 1, 0), cf.Position)
  186.                         cc.Parent = wearingArmorButtons[equipped.Name].ModelViewportFrame
  187.                         wearingArmorButtons[equipped.Name].ModelViewportFrame.CurrentCamera = cc
  188.                        
  189.                         wearingArmorButtons[equipped.Name].ModelImageLabel.Visible = false
  190.                         wearingArmorButtons[equipped.Name].ModelViewportFrame.Visible = true
  191.  
  192.                     else
  193.                         wearingArmorButtons[equipped.Name].ModelImageLabel.Image = armorpiece:WaitForChild("Stats").Icon.Value
  194.  
  195.                         wearingArmorButtons[equipped.Name].ModelViewportFrame.Visible = false
  196.                         wearingArmorButtons[equipped.Name].ModelImageLabel.Visible = true
  197.                     end
  198.  
  199.                     wearingArmorButtons[equipped.Name].NameTextLabel.Text = equipped.Value.Name
  200.                     wearingArmorButtons[equipped.Name].NameTextLabel.Visible = true
  201.                    
  202.                     break
  203.                 end
  204.             end
  205.         end
  206.     end
  207. end
  208.  
  209. updateInv()
  210. myInv.ChildAdded:Connect(updateInv)
  211. myInv.ChildRemoved:Connect(updateInv)
  212. for _, child in pairs(myEquipped:GetChildren()) do
  213.     child:GetPropertyChangedSignal("Value"):Connect(updateInv)
  214. end
  215.  
  216.  
  217. --Unequipping armor
  218. for armortype, btn in pairs(wearingArmorButtons) do
  219.     btn.MouseButton1Click:Connect(function()
  220.         if btn.NameTextLabel.Visible == true then
  221.             removeRE:FireServer(armortype)
  222.         end
  223.     end)
  224. end
  225.  
  226.  
  227. --Opening and closing inventory
  228. btn.MouseButton1Click:Connect(function()
  229.     frame.Visible = not frame.Visible
  230. end)
  231.  
  232. closebtn.MouseButton1Click:Connect(function()
  233.     frame.Visible = false
  234. end)
  235.  
  236.  
  237. --Character viewer
  238. game:GetService("RunService").Heartbeat:Connect(function()
  239.    
  240.     if frame.Visible == true then
  241.         local char = game.Players.LocalPlayer.Character
  242.         if char.Archivable == false then
  243.             char.Archivable = true
  244.         end
  245.        
  246.         if not charvpf:FindFirstChild("CurrentCamera") then
  247.             local cc = Instance.new("Camera")
  248.             cc.Name = "CurrentCamera"
  249.             cc.Parent = charvpf
  250.             charvpf.CurrentCamera = cc
  251.         end
  252.        
  253.         if charvpf:FindFirstChild("Character") then
  254.             charvpf["Character"]:Destroy()
  255.         end
  256.        
  257.         local newchar = char:Clone()
  258.         newchar.Name = "Character"
  259.         local hrp = newchar:WaitForChild("HumanoidRootPart")
  260.         newchar.Parent = charvpf
  261.        
  262.         charvpf["CurrentCamera"].CFrame = CFrame.new(hrp.Position + (hrp.CFrame.LookVector * 5), hrp.Position)
  263.     end
  264. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement