Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Inventory System Script
- —- Place script in ScreenGui
- —-[[
- ScreenGui
- InventoryFrame (Frame)
- ItemTemplate (Button)
- ]]
- local player = game.Players.LocalPlayer
- local playerGui = player:WaitForChild("PlayerGui")
- local inventoryFrame = playerGui:WaitForChild("ScreenGui"):WaitForChild("InventoryFrame")
- local itemTemplate = inventoryFrame:WaitForChild("ItemTemplate")
- local maxInventorySize = 20
- local inventory = {}
- -- Function to add an item to the inventory
- local function addItem(itemName, quantity)
- for _, item in ipairs(inventory) do
- if item.name == itemName then
- item.quantity = item.quantity + quantity
- item.slot.Button.Text = item.name .. " (" .. item.quantity .. ")"
- return
- end
- end
- if #inventory < maxInventorySize then
- local newItem = {
- name = itemName,
- quantity = quantity,
- slot = itemTemplate:Clone(),
- }
- newItem.slot.Parent = inventoryFrame
- newItem.slot.Button.Text = newItem.name .. " (" .. newItem.quantity .. ")"
- newItem.slot.Visible = true
- table.insert(inventory, newItem)
- updateInventoryLayout()
- else
- print("Inventory full!")
- end
- end
- -- Function to remove an item from the inventory
- local function removeItem(itemName, quantity)
- for index, item in ipairs(inventory) do
- if item.name == itemName then
- item.quantity = item.quantity - quantity
- if item.quantity <= 0 then
- item.slot:Destroy()
- table.remove(inventory, index)
- else
- item.slot.Button.Text = item.name .. " (" .. item.quantity .. ")"
- end
- return
- end
- end
- print("Item not found in inventory!")
- end
- -- Function to update the inventory layout
- local function updateInventoryLayout()
- local padding = 5
- local slotSize = itemTemplate.Size
- local cols = math.floor(inventoryFrame.AbsoluteSize.X / (slotSize.X.Offset + padding))
- for i, item in ipairs(inventory) do
- local row = math.floor((i - 1) / cols)
- local col = (i - 1) % cols
- item.slot.Position = UDim2.new(0, col * (slotSize.X.Offset + padding), 0, row * (slotSize.Y.Offset + padding))
- end
- end
- -- Function to handle item usage
- local function useItem(itemName)
- for _, item in ipairs(inventory) do
- if item.name == itemName then
- print("Using item:", itemName)
- removeItem(itemName, 1)
- return
- end
- end
- print("Item not found in inventory!")
- end
- -- Adding drag and drop functionality
- local function enableDragAndDrop()
- for _, item in ipairs(inventory) do
- local slot = item.slot
- local dragging = false
- local dragStart, startPos
- local function updateDrag(input)
- local delta = input.Position - dragStart
- slot.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
- end
- slot.InputBegan:Connect(function(input)
- if input.UserInputType == Enum.UserInputType.MouseButton1 then
- dragging = true
- dragStart = input.Position
- startPos = slot.Position
- input.Changed:Connect(function()
- if input.UserInputState == Enum.UserInputState.End then
- dragging = false
- updateInventoryLayout() -- Re-organize layout after drop
- end
- end)
- end
- end)
- slot.InputChanged:Connect(function(input)
- if dragging and input.UserInputType == Enum.UserInputType.MouseMovement then
- updateDrag(input)
- end
- end)
- end
- end
- -- Example usage
- addItem("Health Potion", 5)
- addItem("Mana Potion", 3)
- useItem("Health Potion")
- enableDragAndDrop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement