Changing The Size Of Scrolling Frames Using Scripts

I have a script that makes the scrolling frame larger when more items are added to it; basically a pet inventory system. However, when this part of the script runs it changes the sizing of the contents inside of the scrolling frame.

Canvas.CanvasSize = UDim2.new(0, Constraint.AbsoluteContentSize.X, 0, Constraint.AbsoluteContentSize.Y)

Without the script
image

With the script
image

Full Script

local player = game.Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShowPet = ReplicatedStorage:WaitForChild("PurchasePrompts").ShowPet

local scrollingFrame = script.Parent

local canvas = script.Parent
local contraint = script.Parent:WaitForChild("UIGridLayout")

local function UpdateCanvasSize(Canvas, Constraint)--RESET THE CANVAS SIZE
	Canvas.CanvasSize = UDim2.new(0, Constraint.AbsoluteContentSize.X, 0, Constraint.AbsoluteContentSize.Y)---THIS IS THE LINE THAT's AFFECTING IT
end

local function addTemplate(pet)
	print("adding template")
	local newTemplate = script.PetTemplates:FindFirstChild(pet):Clone()
	newTemplate.Name = pet
	newTemplate.Parent = scrollingFrame

	

	UpdateCanvasSize(canvas, contraint)
end

ShowPet.OnClientEvent:Connect(addTemplate) 

Explorer

image

Thanks!

(When I increase the Y size of the scrolling frame canvas manually and add the template in, it’s the correct size)