Using AbsoluteSize to offset a frame gives weird results

hey there, I’ve been working on a username system that includes a device icon beside the tag. I’ve wrote a script to offset the icon frame, this is how it basically works:

  1. calculate length of username using TextService:GetTextSize()
  2. get the textlabel’s x absolutesize, then add that with the iconSize’s half
  3. convert offset to scale
  4. clamp the value so it doesn’t go beyond the gui’s bounds

although this seemed kinda simple to me, it’s yielding very inconsistent results, sometimes it’ll yield 0.69, 0.7, 0.83000001 or 0.63

source code:

local textService = game:GetService("TextService")

local playerTag = script.Parent
local deviceIcon = playerTag.Parent:WaitForChild("DeviceIcon")

local displayName, displayText = playerTag.Display, playerTag.Display:WaitForChild("TextLabel")
local userName, userText = playerTag.Username, playerTag.Username:WaitForChild("TextLabel")

local iconSize = deviceIcon.AbsoluteSize

local function getTextBounds()
	local message = displayText.Text
	local size = displayText.AbsoluteSize
	local bounds = textService:GetTextSize(message, 26, Enum.Font.Montserrat, size)
	return (bounds + Vector2.new(1, 1)) / 2
end

local function convertOffset(offset, absSize)
	local scale = offset / absSize
	scale = scale * 100
	scale = math.ceil(scale)
	scale = scale / 100
	return scale --round up to 2 decimals
end

local function offsetDeviceIcon()
	repeat task.wait() until displayName.AbsoluteSize.X ~= 0

	print("X not 0")
	print("double checking: ".. displayName.AbsoluteSize.X)
	
	local absX = displayName.AbsoluteSize.X
	local absY = displayName.AbsoluteSize.Y
	
	local offset1 = getTextBounds().X + (iconSize.X / 2)
	local offset2 = convertOffset(offset1, absX) + 0.5
	local offset3 = math.clamp(offset2, 0.5, 1)
	
	print("Offset 1: ".. offset1)
	print("Offset 2: ".. offset2)
	print("Offset 3: ".. offset3)
	return offset3
end

deviceIcon.Position = UDim2.new(offsetDeviceIcon(), 0, deviceIcon.Position.Y.Scale, 0)

--for i = 1, 5 do
--	deviceIcon.Position = UDim2.new(offsetDeviceIcon(), 0, deviceIcon.Position.Y.Scale, 0)
--	task.wait(3 * i)
--end
--print("abs size: ".. absX)
--print(offset2)
--deviceIcon.Position = UDim2.

results:


I have no idea why this happens, and I’ve spent so long trying to debug it, so if anyone could help me out, that’d be great, thank you!

1 Like