CanvasPosition Problem

I am currently working on a radio, as soon as the chat frame fills up I’d like the canvasposition to increase. As you see in the script below that’s what I’m having it do when chat frame fills up. (Basically when offset is greater than 200) But here it doesn’t seem to be changing at all and I can’t quite figure it out at all.

local function createNewMessage(username, message, team)
	local userPrefix = getPlayerPrefix(team).Prefix
	local newUsername = '['..userPrefix..']'..username..': '
	
	local newMsgFrame = MsgFrame:Clone()
	local requiredSpaces = GetNumberOfSpaces(newUsername, Enum.Font.SourceSansBold, 24)
	
	newMsgFrame.Prefix.Text = newUsername
	newMsgFrame.Prefix.TextColor3 = getPlayerPrefix(team).Color
	
	newMsgFrame.Msg.Text = (" "):rep(requiredSpaces)..message
	newMsgFrame.Parent = ChatFrame
	
	if PrevMsg then
		local dif = newMsgFrame.Position.Y.Offset - PrevMsg.Position.Y.Offset
		newMsgFrame.Position = UDim2.new(0, 0, 0, PrevMsg.Position.Y.Offset + PrevMsg.Msg.TextBounds.Y*1.3)
		warn(dif)
		ChatFrame.CanvasSize = UDim2.new(0, 0, 0, ChatFrame.CanvasSize.Y.Offset + 1.3)
	else
		ChatFrame.CanvasSize = UDim2.new(0, 0, 0, ChatFrame.CanvasSize.Y.Offset + newMsgFrame.Msg.TextBounds.Y*1.3)
	end
	
	if newMsgFrame.Position.Y.Offset > 200 then
		local labelDifference = newMsgFrame.Position.Y.Offset - PrevMsg.Position.Y.Offset
		ChatFrame.CanvasPosition = Vector2.new(0, ChatFrame.CanvasSize.Y.Offset + 1.3)
		print(ChatFrame.CanvasPosition)
	end
	PrevMsg = newMsgFrame
end

https://gyazo.com/97c9caa1e125294a02126c90e23ad429

Any suggestions or fixes?

1 Like

I suggest using a UIListLayout to automatically position your messages and then getting rid of any of the message frame position setting lines of code. When a new message is added, you can simply set the Y Offset of the CanvasSize to the Y of AbsoluteContentSize of the UIListLayout and then accordingly push the CanvasPosition down to the bottom.

1 Like

Infact, I found this tutorial which explains what @colbert2677 is talking about: