I have a ScrollingFrame and inside a UIListLayout. The game is added new items to the ScrollingFrame and the UIListLayout is of course adding it as a list. The ScrollingFrame scrolls from bottom to top, and the UIListLayout always puts the newest item at the bottom.
When a new item is added, the ScrollingFrame jumps back to the bottom to show the newly added item. How am I able to stop this from happening if a player has scrolled up to view other items? Of course when the player is at the bottom of the ScrollingFrame, I want it to follow when new items are added.
Any tips would be great as I can’t seem to get this to work.
Store the ScrollingFrame’s CanvasPosition before adding a new item, then check if the user was near the bottom (you can do this by comparing CanvasPosition.Y + ScrollingFrame’s AbsoluteSize.Y to the CanvasSize.Y.Offset). If they were near the bottom (for example, within 20 pixels), update the CanvasPosition to show the new item; if not, restore the previous CanvasPosition. This can be implemented by creating a function that handles adding new items.
It should look like this:
local function addItem(item)
-- Get the ScrollingFrame (adjust the path if needed)
local scrollingFrame = script.Parent.ScrollingFrame
-- Check if user is near bottom before adding item
local isAtBottom = (scrollingFrame.CanvasPosition.Y + scrollingFrame.AbsoluteSize.Y + 20 >= scrollingFrame.CanvasSize.Y.Offset)
-- Store current scroll position
local previousPosition = scrollingFrame.CanvasPosition
-- Add the item
item.Parent = scrollingFrame
-- Wait for UIListLayout to update
task.wait()
-- If user wasn't at bottom, restore their scroll position
if not isAtBottom then
scrollingFrame.CanvasPosition = previousPosition
end
end
-- Example usage:
local newItem = Instance.new("TextLabel")
newItem.Size = UDim2.new(1, 0, 0, 50)
newItem.Text = "New Item"
addItem(newItem)
1 Like