0% found this document useful (0 votes)
7 views14 pages

Roblox Inventory Management Script

Uploaded by

Inoob 8C
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Roblox Inventory Management Script

Uploaded by

Inoob 8C
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

-- Services

local Players = game:GetService("Players")


local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local SG = game:GetService("StarterGui")
local HS = game:GetService("HttpService")

-- Modules
local Janitor = require([Link])
local Signal = require([Link])

-- Player Variables
local player = [Link]
local playerGui = player:WaitForChild("PlayerGui")
local character = [Link] or [Link]:Wait()
while not [Link] do
wait()
end
[Link] = 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")
[Link] = true
local infoF = invF:WaitForChild("InfoFrame")
[Link] = false
local instrucT = infoF:WaitForChild("Instructions")
[Link] = false
local searchBar = [Link]
local buttons = [Link]
local sortingb = [Link]
local statF = [Link]
local buttonF = [Link]
local storageT = [Link]

-- Character Viewport
local RNS = game:GetService("RunService")
local BaseCamera =
require([Link]:FindFirstChild("BaseCamera", true))
local DragToRotateViewportFrame =
require([Link])
local playerNameLabel = innerF:WaitForChild("PlayerName")
local playerNameText = playerNameLabel:WaitForChild("PlayerName");
[Link] = character:WaitForChild("Humanoid").DisplayName
local viewportFrame = [Link]

-- 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 = [Link]
local equipbestB = [Link]
local unequipallB = [Link]
-- Inventory Slots
local itemsSF = invF:WaitForChild("Slots")
local itemSample = itemsSF:WaitForChild("Template")
[Link] = 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"),
}

-- Key to Slot Mapping


local keysToSlots = {
[[Link]] = hotbarF:WaitForChild("Slot1"),
[[Link]] = hotbarF:WaitForChild("Slot2"),
[[Link]] = hotbarF:WaitForChild("Slot3"),
[[Link]] = hotbarF:WaitForChild("Slot4"),
[[Link]] = hotbarF:WaitForChild("Slot5"),
[[Link]] = hotbarF:WaitForChild("Slot6"),
[[Link]] = hotbarF:WaitForChild("Slot7"),
[[Link]] = hotbarF:WaitForChild("Slot8"),
[[Link]] = hotbarF:WaitForChild("Slot9"),
}

-- Color Table for Rarities


local RarityColors = {
Common = [Link](212, 212, 212), -- White
Uncommon = [Link](0, 195, 0), -- Green
Rare = [Link](19, 109, 255), -- Blue
Epic = [Link](183, 2, 255), -- Purple
Legendary = [Link](255, 165, 0), -- Orange
Mythic = [Link](255, 217, 0),
}

-- Color Table for Rarities


local SelectedRarityColors = {
Common = [Link](148, 148, 148), -- White
Uncommon = [Link](0, 136, 0), -- Green
Rare = [Link](11, 66, 149), -- Blue
Epic = [Link](105, 0, 166), -- Purple
Legendary = [Link](176, 109, 0), -- Orange
Mythic = [Link](177, 156, 0), -- Pink
}

-- 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 = {}
[Link] = nil
[Link] = nil
[Link] = nil
[Link] = [Link]
[Link] = false
[Link] = MaxStackData
[Link] = RarityColors
[Link] = RarityOrders
[Link] = RarityRankings
[Link] = SelectedRarityColors

-- Viewport
-- Clone the character
local clonedCharacter = character:Clone()

-- Function to remove scripts from the model


local function removeScriptsFromModel(model)
for _, descendant in ipairs(model:GetDescendants()) do
if descendant:IsA("Script") or descendant:IsA("LocalScript") then
descendant:Destroy()
end
end
end

-- Remove scripts from the cloned character


removeScriptsFromModel(clonedCharacter)

local dtrViewportFrame = [Link](viewportFrame)


dtrViewportFrame:SetModel(clonedCharacter)
[Link] = "Default"

[Link]:Connect(function(inputObject)
if [Link] == [Link].MouseButton1 then
local inputObjectChangedC
dtrViewportFrame:BeginDragging()

inputObjectChangedC = [Link]:Connect(function()
if [Link] == [Link] then
inputObjectChangedC:Disconnect()
inputObjectChangedC = nil

dtrViewportFrame:StopDragging()
end
end)
end
end)

---Setting Equip Button


function [Link](toSet)
if toSet == true then
[Link] = "Equip"
equipB.BackgroundColor3 = equipB:GetAttribute("EquipColor")
else
[Link] = "Unequip"
equipB.BackgroundColor3 = equipB:GetAttribute("UnequipColor")
end
end

-- Function to unequip all items


function UnequipAllItems()
-- Send a signal to the server to unequip all items
[Link]("Inventory:UnequipAllItems")
end

-- Colors
local defaultColor = [Link](188, 188, 188)
local selectedColor = [Link](140, 140, 140)

-- Store buttons and their original colors


local sortingButtons = {
All = sortingb:FindFirstChild("All"),
Weapon = sortingb:FindFirstChild("Weapon"),
Armor = sortingb:FindFirstChild("Armor"),
Consumable = sortingb:FindFirstChild("Consumable"),
Resource = sortingb:FindFirstChild("Resource")
}

-- Debug: Print each button reference


for itemType, button in pairs(sortingButtons) do
end

-- Function to update the count of visible items in the ScrollingFrame


local function UpdateVisibleItemsCount()
local visibleCount = 0

-- Iterate through all items in the ScrollingFrame


for _, itemF in pairs(itemsSF:GetChildren()) do
if itemF:IsA("GuiButton") and itemF ~= itemSample then
if [Link] then
visibleCount = visibleCount + 1
end
end
end

-- Update the storageT text with the count


[Link] = tostring(visibleCount)
end

-- Function to filter items by type


local function FilterItemsByType(itemType)
for _, itemF in pairs(itemsSF:GetChildren()) do
if itemF:IsA("GuiButton") and itemF ~= itemSample then
local itemTypeAttr = itemF:GetAttribute("ItemType")
if itemType == "All" or itemTypeAttr == itemType then
[Link] = true
else
[Link] = false
end
end
end
UpdateVisibleItemsCount() -- Update count after filtering
end

-- Function to update button colors


local function UpdateButtonColors(selectedButton)
for _, button in pairs(sortingButtons) do
if typeof(button) == "Instance" and button:IsA("GuiButton") then
if button == selectedButton then
button.BackgroundColor3 = selectedColor
else
button.BackgroundColor3 = defaultColor
end
else
warn("Button is not a valid GUI element or not found: " ..
tostring(button))
end
end
end

-- Connect sorting buttons to the filter and color update functions


for itemType, button in pairs(sortingButtons) do
if button then
button.MouseButton1Click:Connect(function()
FilterItemsByType(itemType)
UpdateButtonColors(button)
end)
else
warn("Button not found for type: " .. itemType)
end
end

-- Variables
local currentCategory = "All" -- Default to "All"

-- Update Results based on Search Bar and Current Category


function UpdateResults()
local search = [Link]([Link])

for i, v in pairs(itemsSF:GetChildren()) do
if v:IsA("GuiButton") and [Link] ~= "Template" then
local itemName = [Link](v:FindFirstChild("NameText").Text)
local itemCategory = v:GetAttribute("ItemType")

local matchesSearch = (search == "") or [Link](itemName,


search)
local matchesCategory = (currentCategory == "All") or
(itemCategory == currentCategory)

[Link] = matchesSearch and matchesCategory


end
end
end

-- Update Current Category and Refresh Results


local function UpdateCategory(newCategory)
currentCategory = newCategory
UpdateResults()
end

-- Connect Search Bar to UpdateResults


searchBar:GetPropertyChangedSignal("Text"):Connect(UpdateResults)

-- Connect Sorting Buttons to UpdateCategory and UpdateResults


for itemType, button in pairs(sortingButtons) do
if button then
button.MouseButton1Click:Connect(function()
UpdateCategory(itemType)
UpdateButtonColors(button)
end)
else
warn("Button not found for type: " .. itemType)
end
end

function [Link](stackData)
-- Update the selected stack ID
local newSelectedStackId = stackData and [Link] or nil

-- Find the corresponding item frame for the current stack


local newItemF = newSelectedStackId and itemsSF:FindFirstChild("Stack-" ..
newSelectedStackId) or nil

-- Flag to check if an item was deselected


local itemDeselected = false

-- Iterate through all children of the items frame


for _, otherItemF in pairs(itemsSF:GetChildren()) do
if otherItemF:IsA("TextButton") and otherItemF ~= itemSample then
-- Get the current colors from attributes
local normalColor = otherItemF:GetAttribute("NormalColor")
local selectedColor = otherItemF:GetAttribute("SelectedColor")

-- Ensure that normalColor and selectedColor are set


if normalColor and selectedColor then
if otherItemF == newItemF then
-- Toggle the color of the clicked item
if otherItemF.BackgroundColor3 == selectedColor then
otherItemF.BackgroundColor3 = normalColor
[Link] = nil --
Deselect the item
itemDeselected = true
else
otherItemF.BackgroundColor3 = selectedColor
[Link] =
newSelectedStackId
end
else
-- Set the color to NormalColor for all other items
otherItemF.BackgroundColor3 = normalColor
end
else
-- Handle the case where attributes might be missing
print("NormalColor or SelectedColor attribute is missing
on", [Link])
end
end
end

-- Updating Information
if not itemDeselected and stackData then
[Link] = true
[Link] = [Link]
[Link] = [Link]
[Link] = [Link]

-- Set [Link] based on ItemTypeDescription or ArmorType


if [Link] then
[Link] = [Link]
elseif [Link] == "Armor" and [Link] then
[Link] = [Link] .. " Armor Piece"
else
[Link] = [Link]
end

[Link] = [Link]
[Link].BackgroundColor3 = RarityColors[[Link]]
[Link].BackgroundColor3 = RarityColors[[Link]]

-- Check if the item is equipped in the hotbar


local isEquipped = false
for slotKey, stackId in pairs([Link]) do
if stackId == [Link] then
isEquipped = true
break
end
end
[Link](not isEquipped)

-- Set drop button visibility based on IsDroppable


if [Link] == false then
[Link] = false
else
[Link] = true
end
else
-- Hide the info frame when the item is deselected or no item is
provided
[Link] = false
[Link](true)

-- Default state for drop button when no item is selected


[Link] = true
end
end

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", [Link])
end
end
end
end

-- Equipping Item
function [Link](slotNum)
if [Link] == slotNum then
[Link]("Inventory:UnequipItems")
else
[Link]("Inventory:EquipItem", slotNum)
end
end

-- Function to Find Stack Data by ID


function [Link](stackId)
for _, stackData in pairs([Link]) do
if [Link] == stackId then
return stackData
end
end
end

-- Checking if Equipped
function [Link](stackData)
if [Link] == "Armor" then
warn("Armor not implemented yet")
else
for slotKey, stackId in pairs([Link]) do
if stackId == [Link] then
return true
end
end
return false
end
end

-- Function to count total number of items in inventory


local function CountTotalItemsInInventory()
local totalItems = 0
for _, stackData in pairs([Link]) do
totalItems = totalItems + [Link]
end
return totalItems
end

-- Function to update visibility of dropallB button based on inventory state


local function UpdateButtonVisibility()
local inventory = [Link]
local itemCount = 0

-- Calculate total number of items in inventory


for _, stackData in pairs(inventory) do
itemCount = itemCount + #[Link]
end

-- Show buttons if there are items in the inventory


[Link] = itemCount > 0
[Link] = itemCount > 0
end

-- Function to Update Inventory Display


function [Link]()
while [Link] do [Link]() end
[Link] = true
UpdateButtonVisibility()
UpdateVisibleItemsCount()
-- Clearing Items
for _, itemF in pairs(itemsSF:GetChildren()) do
if itemF:IsA("TextButton") and itemF ~= itemSample then
itemF:Destroy()
end
end

-- Creating Item Frames


for _, stackData in pairs([Link]) do
local itemF = itemSample:Clone()
[Link] = "Stack-" .. [Link]
[Link] = [Link]

-- Ensure Count is not nil and is a valid number


local itemCount = [Link] or 0
[Link] = tostring(itemCount) .. "x"

[Link] = [Link]
[Link] = RarityOrders[[Link]]
[Link] = [Link](stackData)

local rarityColor = RarityColors[[Link]] or [Link](212, 212,


212)
local selectedrarityColor = SelectedRarityColors[[Link]]
itemF.BackgroundColor3 = rarityColor
itemF:SetAttribute("NormalColor", rarityColor)
itemF:SetAttribute("SelectedColor", selectedrarityColor)
itemF:SetAttribute("ItemType", [Link])
[Link] = [Link]
[Link] = true
-- Handle visibility of count based on itemCount
if itemCount > 1 then
[Link] = true
else
[Link] = false
end

itemF.MouseButton1Click:Connect(function()
[Link](stackData, itemF)
end)
end

-- Updating Hotbar
for slotNum = 1, 9 do
local slotF = hotbarSlots[slotNum]
local stackId = [Link]["Slot" .. slotNum]

if stackId then
local foundStack = [Link](stackId)
if foundStack then
[Link] = [Link]
[Link] = [Link]
[Link] = [Link] > 1
else
[Link] = false
[Link] = ""
end
else
[Link] = false
[Link] = ""
end
end

-- Reselecting Item
local equippedData =
[Link]([Link])
[Link](equippedData)

[Link] = false
end

-- Updating Equipped Item


function [Link]()

-- Character Variables
local char = [Link]; 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 = [Link]["Slot" .. i]
local stackData = [Link](stackId)
if stackData and [Link]([Link], tool) then
slotNum = i
break
end
end

-- Updating
if slotNum then
[Link] = slotNum
local slotF = hotbarSlots[slotNum]
for i, otherSlotF in pairs(hotbarSlots) do
if otherSlotF == slotF then
otherSlotF.BackgroundColor3 =
otherSlotF:GetAttribute("SelectedColor")
[Link]:Play()
else
otherSlotF.BackgroundColor3 =
otherSlotF:GetAttribute("NormalColor")
end
end
else
[Link] = nil
[Link]("Inventory:UnequipItems")
end
else

-- Setting All Back To Normal


for i, slotF in pairs(hotbarSlots) do
do
slotF.BackgroundColor3 = slotF:GetAttribute("NormalColor")
end
[Link] = nil
end
end
end

-- Function to drop all tools


function DropAllTools()
local inventory = [Link]
local toolsDropped = false
for _, stackData in pairs(inventory) do
for _, tool in ipairs([Link]) do
[Link]("Inventory:DropItem", [Link])
toolsDropped = true
end
end

if not toolsDropped then


print("You have no tools to drop.")
-- Add notification or message to inform the player
end
end

-- Function to repeat DropAllTools 3 times


function RepeatDropAllTools()
for i = 1, 3 do
DropAllTools()
end
end
-- Equip Button
function [Link]()
if not [Link] then return end

local stackData =
[Link]([Link])
if [Link] == "Equip" then
local tempJanitor = [Link]()
tempJanitor:GiveChore(function() [Link] = false end)
[Link] = "..."
tempJanitor:GiveChore(function() [Link] = "Equip" end)
[Link] = true

if [Link] == "Armor" then


tempJanitor:Clean()
warn("Armor not implemented yet!")
else
local chosenSlot
local slotNum

tempJanitor:GiveChore([Link]:Connect(function(input,
gameProcessedEvent)
if gameProcessedEvent or [Link] ~=
[Link] then return end
for key, slotF in pairs(keysToSlots) do
if [Link] == key then
chosenSlot = slotF
tempJanitor:Clean()
return
end
end
[Link] = "Error: Not a valid key"
tempJanitor:GiveChore(function() [Link] =
[Link] end)
[Link](2)
tempJanitor:Clean()
end))

for i, slotF in pairs(hotbarSlots) do

tempJanitor:GiveChore(slotF.MouseButton1Click:Connect(function()
chosenSlot = slotF
slotNum = i
tempJanitor:Clean()
end))
end

while not chosenSlot do [Link]() end

if not slotNum then


for i, slotF in pairs(hotbarSlots) do
if slotF == chosenSlot then
slotNum = i
break
end
end
end

[Link]("InventoryEquipToHotbar", slotNum,
[Link])
end
elseif [Link] == "Unequip" then
[Link]("InventoryUnequipFromHotbar", [Link])
end
end

-- Function to Update Inventory Data


function [Link]()
[Link] = [Link]("Inventory:GetInventoryJSON")
end

---Drop button pressed


function [Link]()
if [Link] == nil then return end
---Dropping Item
[Link]("Inventory:DropItem", [Link])

end

-- Input Began Event Handler


function [Link](input, gameProcessedEvent)
if gameProcessedEvent then return end
-- Eqipping Slots
for key, slotF in pairs(keysToSlots) do
if [Link] == key then
[Link]([Link](hotbarSlots, slotF))
end
end
end

function [Link]()
-- Get the inventory data
local inventory = [Link]
if not inventory then return end

-- Create a table to hold the best items for each slot


local bestItems = {}

-- Iterate through the inventory and find the best items


for _, stackData in pairs(inventory) do
-- Ignore armor items
if [Link] ~= "Armor" then
local itemRarity = RarityRankings[[Link]]

-- Determine if the item should be equipped to the hotbar


if itemRarity then
for i = 1, 9 do
if not bestItems[i] or itemRarity >
bestItems[i].rarity then
bestItems[i] = {stackData = stackData, rarity =
itemRarity}
break
end
end
end
end
end
-- Equip the best items to the hotbar
for slotNum, bestItem in pairs(bestItems) do
if bestItem then
[Link]("InventoryEquipToHotbar", slotNum,
[Link])
end
end

-- Update the display to reflect the changes


[Link]()
end

function [Link]()
SG:SetCoreGuiEnabled([Link], false,
[Link], false)
InitializeInventoryColors()
[Link]()
[Link]()
[Link]()
[Link]("Inventory:ItemsUpdated", function(newInvData)
[Link] = newInvData
[Link]()
[Link]()
end)
[Link]:Connect([Link])
equipB.MouseButton1Click:Connect([Link])
dropB.MouseButton1Click:Connect([Link])
dropallB.MouseButton1Click:Connect(function()
RepeatDropAllTools()
end)
unequipallB.MouseButton1Click:Connect(function()
UnequipAllItems()
end)
equipbestB.MouseButton1Click:Connect(function()
[Link]()
end)

-- Connecting Slot Button Equip


for i, slotF in pairs(hotbarSlots) do
slotF.MouseButton1Click:Connect(function()
[Link](i)
end)
end
end

return InventoryClient

You might also like