How to change size of Billboardgui without changing the size of the frame

(I don’t really know if I’m in the right topic, as my gui problem is caused by my script, but please read before saying anything)

Hey.

I’m making a city game, and there’d be a gate/wall that’d have a health bar, and when damaged, the health bar lowers by a script. For some reason, when I run it, the script makes the frame automatically stretches itself to fit the boundaries of the Billboardgui. I then tried to change the size of the billboardgui, but the frame itself changes size.

Before test

During test

I can provide the script if you need it.

Please provide the script. Good idea is to always put it in the post instead of asking if we need it, more info is never a bad thing :slight_smile:

local gate = script.Parent
local humanoid = script.Parent.Humanoid

gate.Touched:Connect(function(hit)
	if hit:IsA("Part")  then
		if hit.Parent:IsA("Tool") then
			if hit.Parent.Name == "Sword" then
				humanoid.Health = humanoid.Health - 10
			end
		end
	end
end)

while true do
	gate.BillboardGui.Frame.Size = UDim2.new(humanoid.Health/200, 0, 2, 0)
	humanoid:GetPropertyChangedSignal("Health"):Connect(function()
		gate.BillboardGui.Frame.TextLabel.Text = humanoid.Health.."/"..humanoid.MaxHealth
	end)
	wait(0.01)
end

I have to go sleep soon, so I need to have a quick solution.
I should also show the size of the billboardgui:

So you are trying to change the size of the billboard gui itself and don’t want the frame to be resized? First of all, why? I fimd it more logical to resize the frame itself, but you may have a reason for that…
Anyway, if you resize the entire gui, and the frame’s size is set in scale(not offset), it will also change size.
I think you could either start setting the frame’s size in offset instead od scale(that way it’s size isn’t going to be depndant on the gui size), or you could just resize the frame itself and not the gui…

Ok, I sized it with offset. However, whenever I zoom out, the gui gets larger for some reason.

Right, i forgot about that, with offset, the frame will always scale to a certain amount of pixels on the users screen, meaning it will resize with distance.
So what you need to do is basically set the frame’s scale directly, not the gui’s scale, may I ask why you were doing that in the first place?

I did set the frame size. I didn’t even touch the billboardgui’s size at all.

Oh right, sorry I misunderstood. Yeah so, your problem is the frame stretching to the entire size of the gui, right?
That is because in the script, you are setting the size to {x, 0, 2,0} where x can be for example 200/200 which is 1. So you are setting the scale to 1 on the x and 2 on the y, when you set the scale to 1, it will set it to the same size as the parent. Scale is a size relative to the parent’s size.
If the scale is 2 on x and 2 on y, it will be set to double the parent’s size on both axis.
Hope this makes sense.

So how do I solve it? I don’t really have much time.

My solution would be:
Use the default size of the frame, the one in the editor before you start and modify it.
In the script, set the size of the frame to
Udim.new((humanoid.Health/200)/frame.Size.X.Scale, 0, frame.Size.Y.Scale, 0)

Edit: and change “frame” to the actual path to the frame.

Like this?

local gate = script.Parent
local humanoid = script.Parent.Humanoid

gate.Touched:Connect(function(hit)
	if hit:IsA("Part")  then
		if hit.Parent:IsA("Tool") then
			if hit.Parent.Name == "Sword" then
				humanoid.Health = humanoid.Health - 10
			end
		end
	end
end)

while true do
	gate.BillboardGui.Frame.Size = UDim2.new((humanoid.Health/200)/gate.BillboardGui.Frame.Frame.Size.X.Scale, 0, gate.BillboardGui.Frame.Frame.Size.Y.Scale, 0)
	humanoid:GetPropertyChangedSignal("Health"):Connect(function()
		gate.BillboardGui.Frame.Frame.TextLabel.Text = humanoid.Health.."/"..humanoid.MaxHealth
	end)
	wait(0.01)
end

Do I have to change the frame size too?

1 Like

Oh, you have two frames? Well, you wanna change the size of the green thing, the one you want to decrease.

1 Like

I did, but half of the green bar is glitching out. robloxapp-20210919-2255470.mp4 (1.0 MB)
You may have to download it and see it from there.

1 Like

Oh I know why, here’s how to fix it.
Instead of this:

gate.BillboardGui.Frame.Size = UDim2.new((humanoid.Health/200)/gate.BillboardGui.Frame.Frame.Size.X.Scale, 0, gate.BillboardGui.Frame.Frame.Size.Y.Scale, 0)

Do this:
First define these BEFORE the for loop, not in it:

local xSize = gate.BillboardGui.Frame.Frame.Size.X.Scale
local ySize = gate.BillboardGui.Frame.Frame.Size.Y.Scale

And then in the for loop do this instead:

gate.BillboardGui.Frame.Size = UDim2.new((humanoid.Health/200)/xSize, 0, ySize, 0)

Try that…

1 Like

Works, but now the green bar is in half. Like you saw on the video, except it stays.

local gate = script.Parent
local humanoid = script.Parent.Humanoid

gate.Touched:Connect(function(hit)
	if hit:IsA("Part")  then
		if hit.Parent:IsA("Tool") then
			if hit.Parent.Name == "Sword" then
				humanoid.Health = humanoid.Health - 10
			end
		end
	end
end)

local xSize = gate.BillboardGui.Frame.Frame.Size.X.Scale
local ySize = gate.BillboardGui.Frame.Frame.Size.Y.Scale

while true do
	gate.BillboardGui.Frame.Frame.Size = UDim2.new((humanoid.Health/200)/xSize, 0, ySize, 0)
	humanoid:GetPropertyChangedSignal("Health"):Connect(function()
		gate.BillboardGui.Frame.Frame.TextLabel.Text = humanoid.Health.."/"..humanoid.MaxHealth
	end)
	wait(0.1)
end

1 Like

What is the max health of the humanoid? If it is not 200, set the “200” in the script to your max health.

2 Likes

Thank you so much! Just in time too, I have to go sleep right now. Thanks you :slight_smile:

2 Likes