Don't use string.len, because some characters are different sizes, so we can't just assume they're all the same. Thankfully, roblox calculates these sizes for us and gives us a method to get those sizes.
To get the length of a string in pixels, you use TextService:GetTextSize
Here's very roughly how you would implement this in your code:
1 | local TextService = game:GetService( "TextService" ) |
3 | local Size = TextService:GetTextSize(message,fontSize,font,chatFrameSize) |
4 | ChatMessageObject.Size = UDim 2. new( 0 ,Size.x, 0 ,Size.y) |