Advertisement
Scripting_King

Untitled

Dec 13th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 KB | Source Code | 0 0
  1. --This is my inventory script
  2.  
  3. -- Services
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5. local Remotes = ReplicatedStorage:WaitForChild("Remotes")
  6. local RunService = game:GetService("RunService")
  7. local module3d = require(ReplicatedStorage.Modules:WaitForChild("Module3D"))
  8.  
  9. -- References to necessary game objects
  10. local EquipEvent = ReplicatedStorage.Remotes:FindFirstChild("EquipEvent")
  11. local Towers = ReplicatedStorage.PlacementSystem:WaitForChild("Bugs")
  12. local player = game.Players.LocalPlayer
  13. local scrollingFrame = player.PlayerGui:WaitForChild("KnifeInventory").Main.WeaponFrame.ScrollingFrame
  14. local InfoFrame = player.PlayerGui.KnifeInventory.Main:WaitForChild("InfoFrame")
  15. local template = ReplicatedStorage.Template:WaitForChild("Template")
  16.  
  17. -- Rarity background images for knives
  18. local COMMON_BG_IMAGE = "rbxassetid://17076760945"
  19. local RARE_BG_IMAGE = "rbxassetid://17076761055"
  20. local LEGENDARY_BG_IMAGE = "rbxassetid://17076761316"
  21. local MYTHIC_BG_IMAGE = "rbxassetid://17076761150"
  22. local LIMITED_BG_IMAGE = "rbxassetid://17076761237"
  23. local DEV_BG_IMAGE = "rbxassetid://17076761417"
  24.  
  25. -- Function to handle the clicking of knife templates
  26. local function onTemplateClick(clickedTemplate, Description, TowerName)
  27.     if clickedTemplate.Equipped.Value == false then
  28.         -- Un-equip other knives and update the background colors
  29.         for _, v in pairs(scrollingFrame:GetChildren()) do
  30.             if v:IsA("GuiButton") then
  31.                 v.Equipped.Value = false
  32.                 v.BackgroundColor3 = Color3.fromRGB(46, 189, 255)
  33.             end
  34.         end
  35.         -- Equip the clicked knife
  36.         clickedTemplate.Equipped.Value = true
  37.         clickedTemplate.BackgroundColor3 = Color3.fromRGB(96, 235, 36) 
  38.        
  39.         -- Update the InfoFrame with knife description and model
  40.         InfoFrame.Description.Text = Description
  41.         InfoFrame.BugName.Text = TowerName
  42.         InfoFrame.ViewPort.Visible = true
  43.         InfoFrame.EquipButton.Visible = true
  44.         InfoFrame.Description.Visible = true
  45.         InfoFrame.BugName.Visible = true
  46.        
  47.         -- Clean up any previous 3D models in the viewport
  48.         for i, child in pairs(InfoFrame.ViewPort:GetChildren()) do
  49.             if child:IsA("ViewportFrame") then
  50.                 child:Destroy()
  51.             end
  52.         end
  53.        
  54.         -- Attach 3D model of the knife to the viewport
  55.         local petModel3d = module3d:Attach3D(InfoFrame:WaitForChild("ViewPort"), Towers:FindFirstChild(TowerName):Clone())
  56.         petModel3d:SetDepthMultiplier(1.6)
  57.         petModel3d.Camera.FieldOfView = 5
  58.         petModel3d.Visible = true
  59.  
  60.         -- Rotate the model in the viewport
  61.         RunService.RenderStepped:Connect(function()
  62.             petModel3d:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
  63.         end)
  64.     else
  65.         -- If the knife is already equipped, un-equip it
  66.         for _, v in pairs(scrollingFrame:GetChildren()) do
  67.             if v:IsA("GuiButton") then
  68.                 v.Equipped.Value = false
  69.                 v.BackgroundColor3 = Color3.fromRGB(46, 189, 255)
  70.             end
  71.         end
  72.     end
  73. end
  74.  
  75. -- Function to create a new template for each knife based on its rarity
  76. local function createTemplate(KnivesName, Description, KnifeRarity)
  77.     -- Clone the template from ReplicatedStorage
  78.     local newTemplate = template:Clone()
  79.     newTemplate.Name = KnivesName
  80.    
  81.     -- Set the rarity background and text color based on knife rarity
  82.     if KnifeRarity == "COMMON" then
  83.         newTemplate.LayoutOrder = 1
  84.         newTemplate.Image = COMMON_BG_IMAGE
  85.         newTemplate.Rarity.TextColor3 = Color3.fromRGB(70,70,70)
  86.     elseif KnifeRarity == "RARE" then
  87.         newTemplate.LayoutOrder = 2
  88.         newTemplate.Image = RARE_BG_IMAGE
  89.         newTemplate.Rarity.TextColor3 = Color3.fromRGB(0, 108, 166)
  90.     elseif KnifeRarity == "LEGENDARY" then
  91.         newTemplate.LayoutOrder = 3
  92.         newTemplate.Image = LEGENDARY_BG_IMAGE
  93.         newTemplate.Rarity.TextColor3 = Color3.fromRGB(231, 193, 0)
  94.     elseif KnifeRarity == "MYTHIC" then
  95.         newTemplate.LayoutOrder = 4
  96.         newTemplate.Image = MYTHIC_BG_IMAGE
  97.         newTemplate.Rarity.TextColor3 = Color3.fromRGB(98, 0, 179)
  98.     elseif KnifeRarity == "LIMITED" then
  99.         newTemplate.LayoutOrder = 5
  100.         newTemplate.Image = LIMITED_BG_IMAGE
  101.         newTemplate.Rarity.TextColor3 = Color3.fromRGB(67, 0, 1)
  102.     elseif KnifeRarity == "DEV" then
  103.         newTemplate.LayoutOrder = 6
  104.         newTemplate.Image = DEV_BG_IMAGE
  105.         newTemplate.Rarity.TextColor3 = Color3.fromRGB(24, 23, 23)
  106.     end
  107.    
  108.     -- Make the new template visible and add it to the scrolling frame
  109.     newTemplate.Visible = true
  110.     newTemplate.Parent = scrollingFrame
  111.    
  112.     -- Set the knife name and rarity in the UI
  113.     newTemplate.Rarity.Text = KnifeRarity
  114.     newTemplate.BugName.Text = KnivesName
  115.  
  116.     -- Attach the 3D model of the knife to the viewport in the template
  117.     local petModel3d = module3d:Attach3D(newTemplate:WaitForChild("ViewportFrame"), Towers:FindFirstChild(KnivesName):Clone())
  118.     petModel3d:SetDepthMultiplier(1.6)
  119.     petModel3d.Camera.FieldOfView = 5
  120.     petModel3d.Visible = true
  121.  
  122.     -- Rotate the model in the viewport
  123.     RunService.RenderStepped:Connect(function()
  124.         petModel3d:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
  125.     end)
  126.  
  127.     -- Connect the button click to equip/unequip the knife
  128.     newTemplate.MouseButton1Click:Connect(function()
  129.         onTemplateClick(newTemplate, Description, KnivesName)
  130.     end)
  131. end
  132.  
  133. -- Listen for the EquipEvent and create the corresponding knife template
  134. EquipEvent.OnClientEvent:Connect(function(Knife)
  135.     createTemplate(Knife.Name, Knife.Description.Value, Knife.Rarity.Value)
  136. end)
  137.  
  138. -- Function to retrieve all player data and populate the inventory
  139. local function getAllPlayerData()
  140.     local player = game.Players.LocalPlayer
  141.     if not player then
  142.         return
  143.     end
  144.  
  145.     -- Request all player data from the server
  146.     local allData = Remotes.GetAllData:InvokeServer(player)
  147.  
  148.     -- Wait until all data is received
  149.     repeat
  150.         wait()
  151.         allData = Remotes.GetAllData:InvokeServer(player)
  152.     until allData and #allData.Towers > 0
  153.  
  154.     -- If data is successfully received, create templates for each tower
  155.     if allData then
  156.         for i, child in pairs(allData.Towers) do
  157.             local Knife = ReplicatedStorage.PlacementSystem.Bugs:FindFirstChild(child)
  158.             createTemplate(child, Knife.Description.Value, Knife.Rarity.Value)
  159.         end
  160.     else
  161.         print("Failed to retrieve all player data.")
  162.     end
  163. end
  164.  
  165. -- Call the function to populate the inventory with the player's knives
  166. getAllPlayerData()
  167.  
  168. --Equip or unequip knife when the EquipButton is clicked in InfoFrame
  169. InfoFrame.EquipButton.MouseButton1Click:Connect(function()
  170.     local selectedKnife = InfoFrame.BugName.Text
  171.     local equipped = false
  172.     -- Check if the selected knife is already equipped
  173.     for _, v in pairs(scrollingFrame:GetChildren()) do
  174.         if v:IsA("GuiButton") and v.BugName.Text == selectedKnife then
  175.             equipped = v.Equipped.Value
  176.             break
  177.         end
  178.     end
  179.    
  180.     -- Toggle equip/unequip state
  181.     if equipped then
  182.         InfoFrame.EquipButton.Text = "Equip"
  183.         InfoFrame.EquipButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
  184.     else
  185.         InfoFrame.EquipButton.Text = "Unequip"
  186.         InfoFrame.EquipButton.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
  187.     end
  188. end)
  189.  
Tags: #scripting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement