Soo i’m working on client-side inventory part, it’s terrible, i never liked it but now i have dilemma, is this script optimized? and are there any memory leaks? i’m afraid it will lag players when they will use inventory, or that some may use it for cheating
local Render = {
["Slots"] = {}
}
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local System = script.Parent
-- Replication
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Inventories = ReplicatedStorage.Inventories
local PlayerInventory = Inventories:WaitForChild(player.UserId)
local Inventory = PlayerInventory:WaitForChild("Inventory",8)
local SingleItems = PlayerInventory:WaitForChild("SingleItems",8)
local Armor = PlayerInventory:WaitForChild("Armor",8)
-- Gui
local InteractionGui = player.PlayerGui:WaitForChild("InteractionGui")
local Main = InteractionGui.Main
local UseButton = Main.UseButton
local DropButton = Main.DropButton
-- Modules
local ItemData = require(game:GetService("ReplicatedStorage").ObjectsData.ItemsData)
local Interactions = require(System.Interactions)
-- Important
local debounce = true
local PickAndDrop = ReplicatedStorage.Remotes.PickAndDrop
local ItemUse = ReplicatedStorage.Remotes.ItemUse
-- Creates new frame and returns it
function Render:CreateItem(Item: ValueBase) : GuiBase
local New = System.Item:Clone()
New.Name = Item.Name
New.Image = ItemData[Item.Name].Image
Render["Slots"][Item] = New
if Item.ClassName == "IntValue" then New.Count.Text = Item.Value else New.Count.Text = "1" end
return New
end
-- Enables/Disables interaction gui
function Render:InteractionGui(Position: Vector2, Item: ValueBase) : GuiBase
if Item.ClassName == "BoolValue" and Item.Value == true then return end
InteractionGui.Enabled = true
if not Position then InteractionGui.Enabled = false; InteractionGui.Inspecting.Value = nil; return end
Main.Position = UDim2.new(0,Position.X,0,Position.Y)
if ItemData[Item.Name].Class == "Item" then Main.UseButton.Visible = false else Main.UseButton.Visible = true end
InteractionGui.Inspecting.Value = Item
Main.TextLabel.Text = Item.Name
end
-- Drops item that gui currently is inspecting
local function Drop()
local Item = InteractionGui.Inspecting.Value
if Item and debounce then
debounce = false
PickAndDrop:FireServer(Item)
if Item.ClassName == "IntValue" and Item.Value <= 1 or Item.Parent == SingleItems then InteractionGui.Enabled = false end
task.wait(0.1)
debounce = true
end
end
-- Uses inspected item
local function Use()
local Item = InteractionGui.Inspecting.Value
if Item and debounce then
debounce = false
local Class = ItemData[Item.Name].Class
local Frame = Render.Slots[Item]
local UseFunction = Interactions[Class]
if UseFunction then UseFunction(Item, Frame) end
game:GetService("ReplicatedStorage").Remotes.ItemUse:FireServer(Item, mouse.Hit.Position)
InteractionGui.Enabled = false
task.wait(0.1)
debounce = true
end
end
DropButton.MouseButton1Down:Connect(Drop)
UseButton.MouseButton1Down:Connect(Use)
return Render