Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Using a script, how do I change the size of Y in a gui?

This is a script that changes a custom health bar's color and size when damaged. The script detects when the enemy humanoid's health is changed, and changes the custom health bar color.

But I need the size of Y in the Gui to change. I could do ,0,0,12 next to "char.MaxHealth" and it works, but it looks funky and changes size depending on your camera position and how close it is to the Gui health bar.

I want the 0,0,12 to be {0, 0},{0.6, 0} which will fix the problem, but I don't know how to format it after "char.MaxHealth"

local char = script.Parent.Parent.Parent.Parent.Parent.Humanoid

char.Changed:Connect(function()
	local redzone = char.MaxHealth/2.5
	if char.Health <= redzone then
		script.Parent.ImageColor3 = Color3.fromRGB(255,0,0)
	end
	if char.Health > redzone then
		script.Parent.ImageColor3 = Color3.fromRGB(41, 255, 119)
	end
	script.Parent.Size = UDim2.new(char.Health/char.MaxHealth({0, 0},{0.6, 0})) --The problem. 
end)

Error, "Workspace.GummyBear.HealthBar.HealthGui.HealthFrame.Health.HealthScript:11: attempt to call a number value"

2 answers

✓ Accepted answer

one of the reasons for the error is actually b/c of script.Parent.Size = UDim2.new(char.Health/char.MaxHealth( ... ) this implies that your calling a function char.MaxHealth as a function, which we all know is a number, and hence attempt to call a number value in the error message,

another, thing that could cause an issue being, the brackets your putting the numbers in, {0, 0},{0.6, 0}.. Udim2.new actually takes 4 numbers - scaleX, offsetX, scaleY, offsetY, NOT 2 arrays with the numbers group.. you could also use Udim2.fromScale(scaleX, scaleY, which is equal to doing Udim2.new(scaleX, 0, scaleY, 0), and Udim2.fromOffset(offsetX, offsetY) - which is equal to Udim2.fromOffset(0, offsetX,0,offsetY)

so do it like this:

local char = script.Parent.Parent.Parent.Parent.Parent.Humanoid

char.Changed:Connect(function()
    local redzone = char.MaxHealth/2.5
    if char.Health <= redzone then
        script.Parent.ImageColor3 = Color3.fromRGB(255,0,0)
    end
    if char.Health > redzone then
        script.Parent.ImageColor3 = Color3.fromRGB(41, 255, 119)
    end
    script.Parent.Size = UDim2.new(char.Health/char.MaxHealth, 0, .6, 0)
end)

or

local char = script.Parent.Parent.Parent.Parent.Parent.Humanoid

char.Changed:Connect(function()
    local redzone = char.MaxHealth/2.5
    if char.Health <= redzone then
        script.Parent.ImageColor3 = Color3.fromRGB(255,0,0)
    end
    if char.Health > redzone then
        script.Parent.ImageColor3 = Color3.fromRGB(41, 255, 119)
    end
    script.Parent.Size = UDim2.fromScale(char.Health/char.MaxHealth, .6)
end)

2 comments

0

stop

0

YES THANKS SO MUCH. The first option worked!! I tried the second but it didn't work.

local char = script.Parent.Parent.Parent.Parent.Parent.Humanoid

char.Changed:Connect(function()
    local redzone = char.MaxHealth/2.5
    if char.Health <= redzone then
        script.Parent.ImageColor3 = Color3.fromRGB(255,0,0)
    end
    if char.Health > redzone then
        script.Parent.ImageColor3 = Color3.fromRGB(41, 255, 119)
    end
    script.Parent.Size = UDim2.new(char.Health/char.MaxHealth,{0.6, 0}))
end)

3 comments

0

Doesn't work, gives me the same error :/

0

huh

0

Sorry never mind this.

This discussion is preserved as it was. New answers, comments, and votes are closed.