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

[Solved]Health Bar X Size getting bigger then Screen?

Hello everyone,

i have a problem with my screen bar if the player life is getting higher(power value = maxhealth+powervalue) then my health bar is sizing to much (Screen Shot) maybe someone see the error or soluation ?

local Players = game:GetService("Players")
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()
local character = player.Character or player.CharacterAdded:wait()

local PlayerLevel = player:WaitForChild("leaderstats").Level.Value
local human = character:FindFirstChild("Humanoid")
local health = human.Health
local maxhealth = human.MaxHealth
local PlayerXP = player:WaitForChild("playerdata"):WaitForChild("XP")

local xpToLevelUp = function(level)
	return 100 + level * 5
end

while wait() do
	local pourcentage = math.floor(player:WaitForChild("playerdata"):WaitForChild("XP").Value/xpToLevelUp(player:WaitForChild("leaderstats"):WaitForChild("Level").Value) * 100)
	if health then
		script.Parent:TweenSize(UDim2.new(player:WaitForChild("playerdata"):WaitForChild("XP").Value/xpToLevelUp(player:WaitForChild("leaderstats"):WaitForChild("Level").Value), 0, 1, 0))
		script.Parent.Parent.TextLabel.Text = pourcentage.. "%"
	end
end

#Screenshot

i already tryed something like that

	if health == maxhealth or health > maxhealth then
		script.Parent.Size = UDim2.new(1, 0,1, 0)
	end

3 comments

0

ok i can tell you the issue right now, dont use offset in the sizing, only use scale or it will go outside the frame

0

hes right

0

He's not using Offset he's using Scale

1 answer

✓ Accepted answer

The problem here is you're not checking if the size is greater than the maximum. When using Scale, you want to make sure its not going above 1, or

local xSize = player:WaitForChild("playerdata"):WaitForChild("XP").Value/xpToLevelUp(player:WaitForChild("leaderstats"):WaitForChild("Level").Value)
--If the size is greater than 1, just set it to 1
script.Parent:TweenSize(UDim2.new((xSize < 1 and xSize) or 1, 0, 1, 0))

3 comments

0

this Work so far but i don't know why if i get over 8100 MaxHealth the game put it back to 8100

0

So I think if you wanted their health bars to be the same without it having a lot of issues you can take the damage that you are losing and set it as a percentage to the health they have. Then use that percentage against the scale of 1. So if I had 650/890 health I would set that goal percentage and then tween it to the percentage since scale of 1 is technically 100%

0

Thank you @zemljorog forgot to answer works fine i just posted the experience script and not the health one hehehe :) just had to change xSize Health/MaxHealth thank you for the help

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