(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.
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…
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?
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.
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.
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
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