Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This is my inventory script
- -- Services
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Remotes = ReplicatedStorage:WaitForChild("Remotes")
- local RunService = game:GetService("RunService")
- local module3d = require(ReplicatedStorage.Modules:WaitForChild("Module3D"))
- -- References to necessary game objects
- local EquipEvent = ReplicatedStorage.Remotes:FindFirstChild("EquipEvent")
- local Towers = ReplicatedStorage.PlacementSystem:WaitForChild("Bugs")
- local player = game.Players.LocalPlayer
- local scrollingFrame = player.PlayerGui:WaitForChild("KnifeInventory").Main.WeaponFrame.ScrollingFrame
- local InfoFrame = player.PlayerGui.KnifeInventory.Main:WaitForChild("InfoFrame")
- local template = ReplicatedStorage.Template:WaitForChild("Template")
- -- Rarity background images for knives
- local COMMON_BG_IMAGE = "rbxassetid://17076760945"
- local RARE_BG_IMAGE = "rbxassetid://17076761055"
- local LEGENDARY_BG_IMAGE = "rbxassetid://17076761316"
- local MYTHIC_BG_IMAGE = "rbxassetid://17076761150"
- local LIMITED_BG_IMAGE = "rbxassetid://17076761237"
- local DEV_BG_IMAGE = "rbxassetid://17076761417"
- -- Function to handle the clicking of knife templates
- local function onTemplateClick(clickedTemplate, Description, TowerName)
- if clickedTemplate.Equipped.Value == false then
- -- Un-equip other knives and update the background colors
- for _, v in pairs(scrollingFrame:GetChildren()) do
- if v:IsA("GuiButton") then
- v.Equipped.Value = false
- v.BackgroundColor3 = Color3.fromRGB(46, 189, 255)
- end
- end
- -- Equip the clicked knife
- clickedTemplate.Equipped.Value = true
- clickedTemplate.BackgroundColor3 = Color3.fromRGB(96, 235, 36)
- -- Update the InfoFrame with knife description and model
- InfoFrame.Description.Text = Description
- InfoFrame.BugName.Text = TowerName
- InfoFrame.ViewPort.Visible = true
- InfoFrame.EquipButton.Visible = true
- InfoFrame.Description.Visible = true
- InfoFrame.BugName.Visible = true
- -- Clean up any previous 3D models in the viewport
- for i, child in pairs(InfoFrame.ViewPort:GetChildren()) do
- if child:IsA("ViewportFrame") then
- child:Destroy()
- end
- end
- -- Attach 3D model of the knife to the viewport
- local petModel3d = module3d:Attach3D(InfoFrame:WaitForChild("ViewPort"), Towers:FindFirstChild(TowerName):Clone())
- petModel3d:SetDepthMultiplier(1.6)
- petModel3d.Camera.FieldOfView = 5
- petModel3d.Visible = true
- -- Rotate the model in the viewport
- RunService.RenderStepped:Connect(function()
- petModel3d:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
- end)
- else
- -- If the knife is already equipped, un-equip it
- for _, v in pairs(scrollingFrame:GetChildren()) do
- if v:IsA("GuiButton") then
- v.Equipped.Value = false
- v.BackgroundColor3 = Color3.fromRGB(46, 189, 255)
- end
- end
- end
- end
- -- Function to create a new template for each knife based on its rarity
- local function createTemplate(KnivesName, Description, KnifeRarity)
- -- Clone the template from ReplicatedStorage
- local newTemplate = template:Clone()
- newTemplate.Name = KnivesName
- -- Set the rarity background and text color based on knife rarity
- if KnifeRarity == "COMMON" then
- newTemplate.LayoutOrder = 1
- newTemplate.Image = COMMON_BG_IMAGE
- newTemplate.Rarity.TextColor3 = Color3.fromRGB(70,70,70)
- elseif KnifeRarity == "RARE" then
- newTemplate.LayoutOrder = 2
- newTemplate.Image = RARE_BG_IMAGE
- newTemplate.Rarity.TextColor3 = Color3.fromRGB(0, 108, 166)
- elseif KnifeRarity == "LEGENDARY" then
- newTemplate.LayoutOrder = 3
- newTemplate.Image = LEGENDARY_BG_IMAGE
- newTemplate.Rarity.TextColor3 = Color3.fromRGB(231, 193, 0)
- elseif KnifeRarity == "MYTHIC" then
- newTemplate.LayoutOrder = 4
- newTemplate.Image = MYTHIC_BG_IMAGE
- newTemplate.Rarity.TextColor3 = Color3.fromRGB(98, 0, 179)
- elseif KnifeRarity == "LIMITED" then
- newTemplate.LayoutOrder = 5
- newTemplate.Image = LIMITED_BG_IMAGE
- newTemplate.Rarity.TextColor3 = Color3.fromRGB(67, 0, 1)
- elseif KnifeRarity == "DEV" then
- newTemplate.LayoutOrder = 6
- newTemplate.Image = DEV_BG_IMAGE
- newTemplate.Rarity.TextColor3 = Color3.fromRGB(24, 23, 23)
- end
- -- Make the new template visible and add it to the scrolling frame
- newTemplate.Visible = true
- newTemplate.Parent = scrollingFrame
- -- Set the knife name and rarity in the UI
- newTemplate.Rarity.Text = KnifeRarity
- newTemplate.BugName.Text = KnivesName
- -- Attach the 3D model of the knife to the viewport in the template
- local petModel3d = module3d:Attach3D(newTemplate:WaitForChild("ViewportFrame"), Towers:FindFirstChild(KnivesName):Clone())
- petModel3d:SetDepthMultiplier(1.6)
- petModel3d.Camera.FieldOfView = 5
- petModel3d.Visible = true
- -- Rotate the model in the viewport
- RunService.RenderStepped:Connect(function()
- petModel3d:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
- end)
- -- Connect the button click to equip/unequip the knife
- newTemplate.MouseButton1Click:Connect(function()
- onTemplateClick(newTemplate, Description, KnivesName)
- end)
- end
- -- Listen for the EquipEvent and create the corresponding knife template
- EquipEvent.OnClientEvent:Connect(function(Knife)
- createTemplate(Knife.Name, Knife.Description.Value, Knife.Rarity.Value)
- end)
- -- Function to retrieve all player data and populate the inventory
- local function getAllPlayerData()
- local player = game.Players.LocalPlayer
- if not player then
- return
- end
- -- Request all player data from the server
- local allData = Remotes.GetAllData:InvokeServer(player)
- -- Wait until all data is received
- repeat
- wait()
- allData = Remotes.GetAllData:InvokeServer(player)
- until allData and #allData.Towers > 0
- -- If data is successfully received, create templates for each tower
- if allData then
- for i, child in pairs(allData.Towers) do
- local Knife = ReplicatedStorage.PlacementSystem.Bugs:FindFirstChild(child)
- createTemplate(child, Knife.Description.Value, Knife.Rarity.Value)
- end
- else
- print("Failed to retrieve all player data.")
- end
- end
- -- Call the function to populate the inventory with the player's knives
- getAllPlayerData()
- --Equip or unequip knife when the EquipButton is clicked in InfoFrame
- InfoFrame.EquipButton.MouseButton1Click:Connect(function()
- local selectedKnife = InfoFrame.BugName.Text
- local equipped = false
- -- Check if the selected knife is already equipped
- for _, v in pairs(scrollingFrame:GetChildren()) do
- if v:IsA("GuiButton") and v.BugName.Text == selectedKnife then
- equipped = v.Equipped.Value
- break
- end
- end
- -- Toggle equip/unequip state
- if equipped then
- InfoFrame.EquipButton.Text = "Equip"
- InfoFrame.EquipButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
- else
- InfoFrame.EquipButton.Text = "Unequip"
- InfoFrame.EquipButton.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement