So I have it works so that when you get xp green bar is filled, but when you leveled up the bar does not go back, here’s the script, it is local, self-filling works but retracting it does not
local player = game:GetService("Players").LocalPlayer
local exp = player.Data.Exp
local requiredExp = player.Data.RequiredExp
local lvl = player.Data.Level
exp.Changed:Connect(function(changed)
if changed then
script.Parent.BackgroundFrame.ExpFrame:TweenSize(UDim2.new(exp.Value / requiredExp.Value, 0, 1.310, 0))
if exp.Value == 0 then
print("1")
script.Parent.BackgroundFrame.ExpFrame:TweenSize(UDim2.new(exp.Value, 0, 1.310, 0))
end
end
end)
while wait() do
script.Parent.ExpLabel.Text = exp.Value.. "/".. requiredExp.Value
end
Because you arent checking if the Exp is greater than the required Exp, if you want it simply, just check if the exp is greater or equal to the required exp, and if so subtract the exp by the required exp, like so:
-- Server:
if exp.Value >= requiredExp.Value then -- if exp is greater than required
exp.Value -= requiredExp.Value -- subtract exp with required
print("Level Up")
end
-- Client:
script.Parent.BackgroundFrame.ExpFrame:TweenSize(UDim2.new(exp.Value / requiredExp.Value, 0, 1.310, 0))
Handle this on the Server first of all, because the Client cannot make these types of changes for the Server, plus its safer, unless you already have that kind of system
You should remove the while loop, instead use the .Changed Event.