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)
