The VirtualScroller
is a Roblox Lua module designed for efficient scrolling through large datasets in a UI ScrollingFrame. It uses virtualization to only render items that are currently visible (plus a buffer), reducing performance overhead. Ideal for lists, grids, or inventories with thousands of items.
Key features:
To install, insert the module script into your Roblox project (e.g., in ReplicatedStorage) and require it in your scripts.
local VirtualScroller = require(ReplicatedStorage.VirtualScroller)
Create a new instance with required parameters. Example:
local VirtualScroller = require(ReplicatedStorage.VirtualScroller)
-- Setup
local scrollingFrame = script.Parent.ScrollingFrame
local itemTemplate = scrollingFrame.ItemTemplate
local data = {} -- e.g., {{Name = "Item1"}, {Name = "Item2"}}
local scroller = VirtualScroller.new(
scrollingFrame,
itemTemplate,
data,
50, -- itemHeight
2, -- numColumns
0.5, -- itemWidthScale
15, -- bufferRows
function(frame, data, index)
frame.NameLabel.Text = data.Name or "Item " .. index
end
)
The scroller handles rendering and updates automatically.
Creates a new instance. Parameters after dataTable are optional.
Returns the instance.
Returns visible items count.
local count = scroller:GetVisibleItemCount()
Reloads full data and updates view. (Useful for Inventory Page Changes without multiple ScrollingFrames.)
scroller:ReloadData(newData)
Scrolls to index, optionally animated.
scroller:ScrollToIndex(10, true)
Sorts data and updates the Scrollingframe.
scroller:SortData(function(a, b) return a.Name < b.Name end)
Adds item.
scroller:AddItem({Name = "New Item", Id = "?"})
Removes item at index.
scroller:RemoveItem(3)
Cleans up resources.
scroller:Destroy()