Set StudsOffset of BillboardGui based on Model Size

I’m trying to make a custom name tag using a BillboardGui, and I’ve already got the design for it done. It includes a few TextLabels and a ScrollingFrame with some ImageLabels in it.

The problem I have is that setting the StudsOffset of the BillboardGui in Studio so that it is perfectly above my head isn’t really working, as it results in players who do not have accessories having the BillboardGui way too high above their heads. I’m looking for some advice on how I would set the StudsOffset to position above their head, and above their accessories, if they have any.

As you can see below, the name tag sits nicely above my antlers, but this was done manually using the StudsOffset property. However, you can see that my alternate account with no accessories except hair has the BillboardGui placed too high. In game, it looks silly as it is far too high above the head. If they do not own this game’s premium gamepass and aren’t entitled to any of the badges, it looks even more strange as all you can see is the name all the way up there.

I’m not sure if the best way to position the BillboardGui is by using the StudsOffset property; please let me know if there is a better way of doing so.

I’ve thought about a few solutions but they aren’t working as I need. I thought about making an invisible box inside the character which would be the same size as the character, as done below:

script.Parent.Size = game.Workspace.LegendOJ1:GetExtentsSize()

This does make the box and sizes it appropriately, but it only sizes it to where the head is, and doesn’t take into account any accessories. This means it would make no difference just placing the BillboardGui inside the head like I am already doing.


I think this could be done by getting the height of the model and setting the StudsOffset of the BillboardGui based on the height, though I’m unsure of how to get the height of the model with the accessories included.

As I said, I’m looking for some advice on how I would be able to set the StudsOffset of the BillboardGui so that it is just above the head, and takes into account the height of any accessories. Even just being able to get the height of the model with the accessories would be enough for me to set the StudsOffset based on that.

Thanks. :slightly_smiling_face:

Currently I do not believe there is a way to get the height of a model including accessories (someone plz correct me if I’m wrong) which makes this quite difficult to achieve.
The only way I can think of achieving this as of present would be to make a list of headgear that requires a higher placed UI and if you detect the character wearing this headgear you could adjust the hight accordingly.
I am aware though that this is probably not the most convenient or efficient solution but it is the only way around this I can think of as of present.

Just parent the billboard to the head instead of the model.

The BillboardGui is already being parented to the Head of the character. I want to be able to set the StudsOffset based on how high the player’s accessories are, because at the moment it looks strange when a player has no accessories since the nametag appears too high above their head. However, when a player does have accessories, the nametag sits nicely above them.

Using Humanoid:GetAppliedDescription() you can figure out what hats a player is wearing in game.

local HumanoidDescription = Humanoid:GetAppliedDescription()
local Hats = HumanoidDescription.HatAccessory -- this is a comma seperated string value

From here you can interate between the character and find accessories that match their name to eventually find their size. Write a function to calculate the largest one and set the StudsOffset based off the size of that accessory.

I would post an example of that but I’m posting on mobile and am extremely tired, sorry.

1 Like

This is (non-working) code that illustrates how you would loop through all the hats, get the height of their tops relative to the head and pick the highest one.

local tallest = 0 -- The distance from the head to the top of the highest hat.
local head -- head of the character
local posHead = head.Position
local up = head.CFrame.UpVector -- which way is up? obviously (0, 1, 0), but the character could be laying down on the ground while this function is running,

-- Call this with the Handle of an accessory to know how high above the head it is
function getHeight(part)
	local relative = part.Position - posHead -- offset between head and hat
	local distance = up:Dot(relative) -- how much of that offset is "upward"? If up were (0, 1, 0), then this would be the same as relative.Y
	local hatHeight = part.Size.Y / 2 -- last line calculates distance to center of hat, add this to the result to get distance to top of hat
	return distance + hatHeight
end

for _,v in pairs(character:GetChildren()) do
	if IsNotAccessory(v) then return end -- up to you to figure this out
	tallest = math.max(getHeight(v.Handle), tallest) -- increase tallest if we have a new highest hat, or keep it the same
end

-- return the height of the tallest accessory, plus 3 or so studs
return tallest + 3
4 Likes

I edited the example you provided so it fits in my game and it works well now. Thanks to @Eestlane771 and @v7zr for the help.

1 Like