How to make the player name in a custom chat

So I’m making a custom chat and I want the players name to be a different colour. I am using two text labels for this, one with the player’s name and one with the message, and I am calculating the offset required for the players name label. Here is my code

local Message = script.Message:Clone()
Message.PlayerName.Text = playerFrom.Name..": "
Message.PlayerName.Size = UDim2.new(0, #Message.PlayerName.Text * 8, 1, 0)
Message.PlayerName.Position = UDim2.new(0,0,0,0)
Message.Message.Size = UDim2.new(1, 0, 1, 0)
Message.Message.Position = UDim2.new(0, Message.PlayerName.Size.X.Offset, 0, 0)
Message.Message.Text = message

My question is how would I change this to use scale instead of offset? I want it to scale for each screen but I’m not sure how to do it

You can use the parent frame’s property named AbsoluteSize which give the size in offset of the frame.

Dividing Textlabel’s AbsoluteSize with Parent frame’s AbsoluteSize will give you the scale of the textlabel.

-- Here your code
local Scale = Message.PlayerName.AbsoluteSize.X / Message.AbsoluteSize.X
Message.PlayerName.Size = UDim2.new(Scale, 0, 1, 0)

You can do the same for the message scale.

See the documentation for further explanation on this property.

I tried this and it sets the X scale size to nan. The absoluteSize.X of playerName is 0 so that’s probably why. The Explorer looks like this
GlobalMessage - ScrollingFrame
----Message - Frame
--------PlayerName - TextLabel
--------Message - TextLabel
I’ve tried messing around with it but nothing seems to work.

As you already have the offset of PlayerName (through your method of calculation) you can use the offset you found with the AbsoluteSize of your message frame

local MessageOffsetScale = Message.AbsoluteSize.X
local Scale = (#Message.PlayerName.Text * 8) / MessageOffsetScale

Be sure that the frame are visible on the screen before using AbsoluteSize As AbsoluteSize is set when it’s inside the playerGUI

I’ve figured it out I think. I used this

local size = TextService:GetTextSize(Message.PlayerName.Text, Message.PlayerName.TextSize, Message.PlayerName.Font, script.Parent.GlobalMessages.AbsoluteSize)
local Scale = size.X/script.Parent.GlobalMessages.AbsoluteSize.X
Message.PlayerName.Size = UDim2.new(Scale, 0, 1, 0)

and it seems to be working fine.

1 Like