Inventory Client
Inventory Client
-- Modules
local Janitor = require(RS.Modules.Janitor)
local Signal = require(RS.Modules.Signal)
-- Player Variables
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local character = player.Character or player.CharacterAdded:Wait()
while not character.PrimaryPart do
wait()
end
character.Archivable = true
-- Gui
local gui = playerGui:WaitForChild("MainUI")
local hotbarF = gui:WaitForChild("ButtonHolder"):WaitForChild("Hotbar")
local invF = gui:WaitForChild("BackpackFrame")
local sideF = invF:WaitForChild("SideFrame")
local innerF = sideF:WaitForChild("InnerFrame")
invF.Visible = true
local infoF = invF:WaitForChild("InfoFrame")
infoF.Visible = false
local instrucT = infoF:WaitForChild("Instructions")
instrucT.Visible = false
local searchBar = invF.Search
local buttons = infoF.Buttons
local sortingb = invF.SortingButtons
local statF = invF.StatFrame
local buttonF = invF.Buttons
local storageT = statF.Storage
-- Character Viewport
local RNS = game:GetService("RunService")
local BaseCamera =
require(game.Players.LocalPlayer.PlayerScripts:FindFirstChild("BaseCamera", true))
local DragToRotateViewportFrame =
require(game.ReplicatedStorage.Modules.DragToRotateViewportFrame)
local playerNameLabel = innerF:WaitForChild("PlayerName")
local playerNameText = playerNameLabel:WaitForChild("PlayerName");
playerNameText.Text = character:WaitForChild("Humanoid").DisplayName
local viewportFrame = innerF.Player
-- Item Info
local itemNameT = infoF:WaitForChild("ItemName"):WaitForChild("ItemName")
local itemTypeT = infoF:WaitForChild("Type")
local itemDescT = infoF:WaitForChild("Info")
local equipB = buttons:WaitForChild("Equip")
local dropB = buttons:WaitForChild("Drop")
local dropallB = invF.Buttons.Drop
local equipbestB = invF.Buttons.Equip
local unequipallB = invF.Buttons.Unequip
-- Inventory Slots
local itemsSF = invF:WaitForChild("Slots")
local itemSample = itemsSF:WaitForChild("Template")
itemSample.Visible = false
-- Armor
local armorF = innerF:WaitForChild("ArmorFrame")
local headSlot = armorF:WaitForChild("HeadSlot")
local chestSlot = armorF:WaitForChild("ChestSlot")
local feetSlot = armorF:WaitForChild("FeetSlot")
-- Hotbar Slots
local hotbarSlots = {
hotbarF:WaitForChild("Slot1"),
hotbarF:WaitForChild("Slot2"),
hotbarF:WaitForChild("Slot3"),
hotbarF:WaitForChild("Slot4"),
hotbarF:WaitForChild("Slot5"),
hotbarF:WaitForChild("Slot6"),
hotbarF:WaitForChild("Slot7"),
hotbarF:WaitForChild("Slot8"),
hotbarF:WaitForChild("Slot9"),
}
-- Rarity Rankings
local RarityRankings = {
Common = 1,
Uncommon = 2,
Rare = 3,
Epic = 4,
Legendary = 5,
Mythic = 6
}
local RarityOrders = {
Common = 0,
Uncommon = -1,
Rare = -2,
Epic = -3,
Legendary = -4,
Mythic = -5
}
local MaxStackData = {
Weapon = 1,
Armor = 1,
Consumable = 8,
Resource = 16
}
-- Module Definition
local InventoryClient = {}
InventoryClient.InvData = nil
InventoryClient.SelectedStackId = nil
InventoryClient.EquuippedSlotNum = nil
InventoryClient.EquipInstructText = instrucT.Text
InventoryClient.UpdatingDb = false
InventoryClient.MaxStackData = MaxStackData
InventoryClient.RarityColors = RarityColors
InventoryClient.RarityOrders = RarityOrders
InventoryClient.RarityRankings = RarityRankings
InventoryClient.SelectedRarityColors = SelectedRarityColors
-- Viewport
-- Clone the character
local clonedCharacter = character:Clone()
viewportFrame.InputBegan:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
local inputObjectChangedC
dtrViewportFrame:BeginDragging()
inputObjectChangedC = inputObject.Changed:Connect(function()
if inputObject.UserInputState == Enum.UserInputState.End then
inputObjectChangedC:Disconnect()
inputObjectChangedC = nil
dtrViewportFrame:StopDragging()
end
end)
end
end)
-- Colors
local defaultColor = Color3.fromRGB(188, 188, 188)
local selectedColor = Color3.fromRGB(140, 140, 140)
-- Variables
local currentCategory = "All" -- Default to "All"
for i, v in pairs(itemsSF:GetChildren()) do
if v:IsA("GuiButton") and v.Name ~= "Template" then
local itemName = string.lower(v:FindFirstChild("NameText").Text)
local itemCategory = v:GetAttribute("ItemType")
function InventoryClient.SelectItem(stackData)
-- Update the selected stack ID
local newSelectedStackId = stackData and stackData.StackId or nil
-- Updating Information
if not itemDeselected and stackData then
infoF.Visible = true
itemNameT.Text = stackData.Name
itemDescT.Text = stackData.Description
infoF.Image.Image = stackData.Image
infoF.Rarity.Rarity.Text = stackData.Rarity
infoF.Rarity.BackgroundColor3 = RarityColors[stackData.Rarity]
infoF.Image.BackgroundColor3 = RarityColors[stackData.Rarity]
function InitializeInventoryColors()
for _, itemF in pairs(itemsSF:GetChildren()) do
if itemF:IsA("TextButton") and itemF ~= itemSample then
local normalColor = itemF:GetAttribute("NormalColor")
if normalColor then
itemF.BackgroundColor3 = normalColor
else
print("NormalColor attribute is missing on", itemF.Name)
end
end
end
end
-- Equipping Item
function InventoryClient.ToggleEquip(slotNum)
if InventoryClient.EquuippedSlotNum == slotNum then
Signal.FireServer("Inventory:UnequipItems")
else
Signal.FireServer("Inventory:EquipItem", slotNum)
end
end
-- Checking if Equipped
function InventoryClient.CheckItemEquipped(stackData)
if stackData.ItemType == "Armor" then
warn("Armor not implemented yet")
else
for slotKey, stackId in pairs(InventoryClient.InvData.Hotbar) do
if stackId == stackData.StackId then
return true
end
end
return false
end
end
itemF.NameText.Text = stackData.Name
itemF.LayoutOrder = RarityOrders[stackData.Rarity]
itemF.Equipped.Visible = InventoryClient.CheckItemEquipped(stackData)
itemF.MouseButton1Click:Connect(function()
InventoryClient.SelectItem(stackData, itemF)
end)
end
-- Updating Hotbar
for slotNum = 1, 9 do
local slotF = hotbarSlots[slotNum]
local stackId = InventoryClient.InvData.Hotbar["Slot" .. slotNum]
if stackId then
local foundStack = InventoryClient.FindStackDataFromId(stackId)
if foundStack then
slotF.Count.Text = foundStack.Count
slotF.Image.Image = foundStack.Image
slotF.Count.Visible = foundStack.Count > 1
else
slotF.Count.Visible = false
slotF.Image.Image = ""
end
else
slotF.Count.Visible = false
slotF.Image.Image = ""
end
end
-- Reselecting Item
local equippedData =
InventoryClient.FindStackDataFromId(InventoryClient.SelectedStackId)
InventoryClient.SelectItem(equippedData)
InventoryClient.UpdatingDb = false
end
-- Character Variables
local char = player.Character; if not char then return end
local tool = char:FindFirstChildOfClass("Tool")
-- If Tool
if tool then
-- Finding Slot
local slotNum = nil
for i = 1, 9 do
local stackId = InventoryClient.InvData.Hotbar["Slot" .. i]
local stackData = InventoryClient.FindStackDataFromId(stackId)
if stackData and table.find(stackData.Items, tool) then
slotNum = i
break
end
end
-- Updating
if slotNum then
InventoryClient.EquippedSlotNum = slotNum
local slotF = hotbarSlots[slotNum]
for i, otherSlotF in pairs(hotbarSlots) do
if otherSlotF == slotF then
otherSlotF.BackgroundColor3 =
otherSlotF:GetAttribute("SelectedColor")
player.PlayerGui.Sounds.BackpackSound:Play()
else
otherSlotF.BackgroundColor3 =
otherSlotF:GetAttribute("NormalColor")
end
end
else
InventoryClient.EquuippedSlotNum = nil
Signal.FireServer("Inventory:UnequipItems")
end
else
local stackData =
InventoryClient.FindStackDataFromId(InventoryClient.SelectedStackId)
if equipB.Title.Text == "Equip" then
local tempJanitor = Janitor.new()
tempJanitor:GiveChore(function() instrucT.Visible = false end)
equipB.Title.Text = "..."
tempJanitor:GiveChore(function() equipB.Title.Text = "Equip" end)
instrucT.Visible = true
tempJanitor:GiveChore(UIS.InputBegan:Connect(function(input,
gameProcessedEvent)
if gameProcessedEvent or input.UserInputType ~=
Enum.UserInputType.Keyboard then return end
for key, slotF in pairs(keysToSlots) do
if input.KeyCode == key then
chosenSlot = slotF
tempJanitor:Clean()
return
end
end
instrucT.Text = "Error: Not a valid key"
tempJanitor:GiveChore(function() instrucT.Text =
InventoryClient.EquipInstructText end)
task.wait(2)
tempJanitor:Clean()
end))
tempJanitor:GiveChore(slotF.MouseButton1Click:Connect(function()
chosenSlot = slotF
slotNum = i
tempJanitor:Clean()
end))
end
Signal.FireServer("InventoryEquipToHotbar", slotNum,
stackData.StackId)
end
elseif equipB.Title.Text == "Unequip" then
Signal.FireServer("InventoryUnequipFromHotbar", stackData.StackId)
end
end
end
function InventoryClient.EquipBestItems()
-- Get the inventory data
local inventory = InventoryClient.InvData.Inventory
if not inventory then return end
function InventoryClient.Start()
SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false,
Enum.CoreGuiType.Health, false)
InitializeInventoryColors()
InventoryClient.UpdateInventoryData()
InventoryClient.UpdateDisplay()
InventoryClient.UpdatedEquippedItem()
Signal.ListenRemote("Inventory:ItemsUpdated", function(newInvData)
InventoryClient.InvData = newInvData
InventoryClient.UpdateDisplay()
InventoryClient.UpdatedEquippedItem()
end)
UIS.InputBegan:Connect(InventoryClient.InputBegan)
equipB.MouseButton1Click:Connect(InventoryClient.EquipButtonPressed)
dropB.MouseButton1Click:Connect(InventoryClient.DropButtonPressed)
dropallB.MouseButton1Click:Connect(function()
RepeatDropAllTools()
end)
unequipallB.MouseButton1Click:Connect(function()
UnequipAllItems()
end)
equipbestB.MouseButton1Click:Connect(function()
InventoryClient.EquipBestItems()
end)
return InventoryClient