[PLS HELP] How do I make a maximum value?

This is my problem, I am trying to make a system of a maximum value for example, if the player exceeds the value of 160000000 it returns to 160000000 and everything is a loop in the PlayerAdded event and unfortunately it does not work for me.
Someone help me to solve it?

Server Script :

game.Players.PlayerAdded:Connect(function(Player)
	local folder = Player:WaitForChild("Stats")
	
	while wait() do
		if folder.Attack.Value <= 160000000 then
			print("Max stats!")
			folder.Attack.Value = 160000000
		end
	end	
end)

I use translator, I would appreciate your help

There’s a few ways you can. A good example is adding an attribute called “MaxValue” to Attack and then whenever you add to Attack’s value use math.max.

folder.Attack.Value = math.max(folder.Attack.Value + amount, folder.Attack:GetAttribute("MaxValue"))

Changed also works and better than a loop, should try not to use loops if possible.

folder.Attack.Changed:Connect(function (newValue)
    if newValue >= 160000000 then -- Attribute "MaxValue" would also work
        folder.Attack.Value = 160000000
    end
end)
3 Likes