Auto-scroll to new frame added

I have a script where is scrolls to the centre of the new frame added to a scrolling frame however after 4 times, it gives up.

local scrollingFrame = script.Parent – Ensure this script is inside the ScrollingFrame

– Function to scroll to the newly added frame
local function scrollToNewFrame(newFrame)
task.wait(0.1) – Small delay to ensure UI updates correctly

-- Calculate the position to scroll to the top of the new frame
local newFrameTopPosition = newFrame.AbsolutePosition.Y

-- Calculate the scroll position
local scrollPosition = newFrameTopPosition - scrollingFrame.AbsolutePosition.Y

-- Ensure we don't scroll past the bottom of the scrollable area
local maxScrollY = scrollingFrame.CanvasSize.Y.Offset - scrollingFrame.AbsoluteSize.Y
scrollPosition = math.min(scrollPosition, maxScrollY)  -- Prevent scrolling past the bottom

-- Set the CanvasPosition to focus on the new frame
scrollingFrame.CanvasPosition = Vector2.new(0, scrollPosition)

end

– Connect to the child added event to focus on the newly added frame
scrollingFrame.ChildAdded:Connect(function(newFrame)
if newFrame:IsA(“GuiObject”) then
scrollToNewFrame(newFrame) – Focus on the new frame
end
end)

1 Like

Hey boss,

My guess is, you’ve read some old code from the past, probably from like 2018. This was the method we utilized; however, ScrollingFrames have a new property you can utilize that doesn’t require code!

aa64b5236dae7b8c12597e305fee268d

ScrollingFrame’s have a property called “AutomaticCanvasSize.” Set that to either NONE, X, Y, or XY. Finally, change the size of the canvas to zero (0).

See this documentation to assist you. Don’t forget to use the object, UIListLayout, to give you more “power” over your scrollingFrame.

ScrollingFrame | Documentation - Roblox Creator Hub

Now the chats go up and delete