Help with XP Bar Script

I’m trying to make an XP bar, but I’m not able to convert the % of the XP out of the MaxXP, into a % of the X on the GUI. I was able to do this with a health bar, but I am struggling with this. The X scale of the GUI is .9.
Local Script:

	local MaxXP = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("MaxXP")
	local XP = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("XP")
	local BlueGui = script.Parent.Blue
	print(game:IsLoaded())
	while wait() do
		local PercentXP  = XP.Value/(MaxXP.Value * .9)--This is what I need fixed
		BlueGui:TweenSize(
			UDim2.fromScale(PercentXP,.05),
			Enum.EasingDirection.Out,
			Enum.EasingStyle.Quad,
			.5,
			true
			)
	end

Server Script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("IntValue",player)
	leaderstats.Name = "leaderstats"
	
	local MaxXP = Instance.new("IntValue",leaderstats)
	MaxXP.Name = "MaxXP"
	MaxXP.Value = 10
	
	local XP = Instance.new("IntValue",leaderstats)
	XP.Name = "XP"
	XP.Value = 0
	
	local Level = Instance.new("IntValue",leaderstats)
	Level.Name = "Level"
	Level.Value = 0
	
	while wait() do
		if XP.Value >= MaxXP.Value then
			MaxXP.Value = MaxXP.Value + 1
			Level.Value = Level.Value + 1
			XP.Value = 0
		end
	end
end)

local Children = game.Workspace:GetChildren()
for i, child in pairs(Children) do
	if child.Name == "Part" then
		child.Touched:Connect(function(part)
			local success,errormessage = pcall(function()
					local player = game.Players:GetPlayerFromCharacter(part.Parent)
						player.leaderstats.XP.Value = player.leaderstats.XP.Value + 1
						child:Destroy()
				end)
						
				
		end)	
	end
end
1 Like

You could multiply the length of the exp bar with the percentXp.

local percentXP = math.floor(XP.Value/MaxXP.Value)
BlueGui:TweenSize(
	UDim2.fromScale((0.9 *PercentXP),.05),
	Enum.EasingDirection.Out,
	Enum.EasingStyle.Quad,
	.5,
    true
)
1 Like

Try out what flashflame suggested, but your main problem is that your math operations are out of order.

XP.Value/(MaxXP.Value * .9) is equivalent to
(10* XP.Value) / (9 * MaxXP.Value)

and you want
(XP.Value/MaxXP.Value) * .9 || (9 * XP.Value)/ (10 * .MaxXPValue)

8 Likes