Greetings,
I have a script that manages a grid inventory system. At this time I can display an ImageLabel within the specified grid’s position, height and width.
Example of 3x3 at position 4:
Example of 1x2 at position 12:
You may be wondering if this works fine then what is the issue? Anytime I attempt to set the width/height higher than 4 it’s like it just goes rogue.
Example of 4x5:
Module:
local modelUI = {}
-- All Items
local bloxCash = {
ID = "$BLX",
Description = "27413",
Width = 4,
Height = 5,
Position = 3
}
-- Load all items into allItems dictionary
local allItems = {}
local function addItem(item)
table.insert(allItems, item)
end
addItem(bloxCash)
-- Insert allItems dictionary into Module
table.insert(modelUI, allItems)
return modelUI
UI:
-- SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ModuleUI = require(ReplicatedStorage:WaitForChild("UI"))
-- VARS
local allItems = ModuleUI[1]
stashMap = {}
local screenMain = script.Parent
local screenInventory = script.Parent:WaitForChild("Inventory")
local frameStash = screenInventory:WaitForChild("Stash")
local stashContainer = frameStash:WaitForChild("Container")
local selection = stashContainer:WaitForChild("selction")
-- Create stash tiles and save their position
local function loadStash()
for i = 1, 150 do
local grid = selection:Clone()
grid.Visible = true
grid.Name = i
grid.Parent = stashContainer
stashMap[i] = grid
end
end
local function calculateGridPositionAndSize(position, width, height, stashMap, stashContainer, columns)
-- Determine the top-left grid cell
local startGrid = stashMap[position]
if not startGrid then
warn("Invalid grid position:", position)
return UDim2.new(0, 0, 0, 0), UDim2.new(0, 0, 0, 0)
end
-- Determine the bottom-right grid cell based on width and height
local endPosition = position + (width - 1) + ((height - 1) * columns)
local endGrid = stashMap[endPosition]
if not endGrid then
warn("End grid cell out of bounds:", endPosition)
return UDim2.new(0, 0, 0, 0), UDim2.new(0, 0, 0, 0)
end
-- Calculate top-left corner position relative to stashContainer
local topLeftPos = startGrid.AbsolutePosition - stashContainer.AbsolutePosition
-- Calculate total size in pixels
local totalWidth = (endGrid.AbsolutePosition.X + endGrid.AbsoluteSize.X) - startGrid.AbsolutePosition.X
local totalHeight = (endGrid.AbsolutePosition.Y + endGrid.AbsoluteSize.Y) - startGrid.AbsolutePosition.Y
-- Convert to scale (UDim2)
local positionScale = UDim2.new(0, topLeftPos.X, 0, topLeftPos.Y)
local sizeScale
if height == 1 then
sizeScale = UDim2.new(0, totalWidth, 0, totalHeight)
else
local adjustX = 51
sizeScale = UDim2.new(0, totalWidth-(adjustX*(height - 1)), 0, totalHeight)
end
return positionScale, sizeScale
end
local function addItemToStash(item)
-- Access item data
local position = item.Position
local width = item.Width
local height = item.Height
-- Validate position
if not stashMap[position] then
warn("Invalid position:", position)
return
end
-- Define grid layout parameters
local columns = 10 -- Adjust based on your grid layout
-- Calculate position and size
local positionScale, sizeScale = calculateGridPositionAndSize(position, width, height, stashMap, stashContainer, columns)
-- Create ImageLabel to represent the item visually
local imageLabel = Instance.new("ImageLabel")
imageLabel.Size = sizeScale
imageLabel.Position = positionScale
imageLabel.BackgroundTransparency = 0
imageLabel.ZIndex = 2
imageLabel.Parent = stashContainer:WaitForChild("ignoredItems")
print("ImageLabel created with Size:", imageLabel.Size, "and Position:", imageLabel.Position)
end
loadStash()
addItemToStash(allItems[1])
You may notice that I have this section of code that adjusts the X value if the height is larger than 1. I did this because if you set the height higher than 2 it also adjusts the width by 1 which is not intended, so I used this to adjust it accordinly.
if height == 1 then
sizeScale = UDim2.new(0, totalWidth, 0, totalHeight)
else
local adjustX = 51
sizeScale = UDim2.new(0, totalWidth-(adjustX*(height - 1)), 0, totalHeight)
end
I’m not sure if my method of calculating the size of the ImageLabel is the most efficient solution and I’m lost as to why it’s inconsistent, any and all help would be much appreciated!
Thanks.