-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryScript.lua
More file actions
48 lines (41 loc) · 1.7 KB
/
InventoryScript.lua
File metadata and controls
48 lines (41 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
local player = game.Players.LocalPlayer
local inventoryButton = script.Parent:WaitForChild("OpenInventoryButton")
local inventoryFrame = script.Parent:WaitForChild("InventoryFrame") -- ScrollingFrame
local closeButton = script.Parent:WaitForChild("CloseButton") -- Close button outside of the ScrollingFrame
-- Function to toggle inventory visibility
local function toggleInventory()
-- Toggle visibility of the inventory
inventoryFrame.Visible = not inventoryFrame.Visible
-- If inventory is visible, toggle the close button
closeButton.Visible = inventoryFrame.Visible
end
-- Connect buttons to toggle function
inventoryButton.MouseButton1Click:Connect(toggleInventory)
closeButton.MouseButton1Click:Connect(toggleInventory)
-- Function to populate inventory UI
local function populateInventory()
-- Clear previous inventory display
for _, child in pairs(inventoryFrame:GetChildren()) do
if child:IsA("TextLabel") then
child:Destroy()
end
end
local yOffset = 0
-- Add a label for each item in the player's inventory
for _, item in pairs(player.Inventory:GetChildren()) do
local itemLabel = Instance.new("TextLabel")
itemLabel.Text = item.Name
itemLabel.Size = UDim2.new(1, 0, 0, 50)
itemLabel.Position = UDim2.new(0, 0, 0, yOffset)
itemLabel.Parent = inventoryFrame
yOffset = yOffset + 50
end
-- Update the CanvasSize to make the ScrollingFrame scrollable
local totalHeight = yOffset
inventoryFrame.CanvasSize = UDim2.new(0, 0, 0, totalHeight)
end
-- Update inventory UI when inventory changes
player.Inventory.ChildAdded:Connect(populateInventory)
player.Inventory.ChildRemoved:Connect(populateInventory)
-- Manually populate the inventory when the player first joins or respawns
populateInventory()