How can I make a max Value leaderstat?

Hello! I am wondering if anyone knows how to make a Leaderstats script with Max Stats. So like the max value is 9999 For coins, and such. This is my current Script, but I want a Max value so it doesn’t bug out. Any help will be appreciated!

	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = stats
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 0
	money.Parent = stats
	
	local eggsopened = Instance.new("IntValue")
	eggsopened.Name = "EggsOpened"
	eggsopened.Value = 0
	eggsopened.Parent = stats
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Value = 0
	gems.Parent = stats
end)

Correct me if I’m wrong, but you could use:

Value.Changed:Connect(function(value) -- Watch for changes in the NumberValue. 
    if value > 9999  then -- if the "value" of the NumberValue > 9999, then set the NumberValue value to 9999.
        value.Value = 9999
    end
end)

You’ll have to connect the .Changed to each of the numbervalues. For example,

eggsopened .Changed:Connect(function(value)

It’s actually pretty simple.

local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = stats
if coins.Value >= 9999 then 
coins.Value = 9999 
end

Like this?

That works too I guess. (30 letters)

Where would I include it inside the script?

That will work for the first time, but You’ll need to watch the value every time it changes, for this to work. You will need to include in the script, like so:

eggsopened.Changed:Connect(function(value)
    if value > 9999  then -- if the "value" of the NumberValue > 9999, then set the NumberValue value to 9999.
       eggsopened.Value = 9999
    end
end)

Include it after the variables are set/parented.

local eggsopened = Instance.new("IntValue")
	eggsopened.Name = "EggsOpened"
	eggsopened.Value = 0
	eggsopened.Parent = stats

eggsopened.Changed:Connect(function(value)
    if value > 9999  then -- if the "value" of the NumberValue > 9999, then set the NumberValue value to 9999.
       eggsopened.Value = 9999
    end
end)
4 Likes

I assume your issue isn’t that you necessarily want a max, but you just don’t want there to be too many characters on the leaderboard.

Instead of using a IntValue, use a StringValue. Then in your script that sets this value, it’s possible to output values like 10k, or 1.2m with some code.

If you truly want a max value, this is something that should be checked before increasing the value. You should never be using these values as your actual data. You should be storing it somewhere (like in a variable in some table for example.)

Then, when it’s time to increase the value:

// some code before...
playerMoney=math.min(playerMoney+AMOUNT, 99999)
money.Value = playerMoney
// more code below...

For some reason at a super high number, the Leaderstats bug out and turn it back to 0, which is why I am trying to find this. It already does 1M and all of that on the leaderboard.

Okay so where do I put this for it to work?

Ah, I wasn’t aware… it’s been a while since I’ve been on roblox :slight_smile:

I just did some checks. The biggest number you can have is 2^62 before it bugs out it seems. I would use that instead of 99999.

Again, you should be checking this value right before you update the value. So lets say you update it like this:

money.Value = money.Value + 100

do this:

money.Value = math.min(money.Value+100, 2^62)

math.min picks the smallest value. So when you are about to increase it over 2^62, it will set the value to that number instead.

I was just using 9999 as a reference haha, sorry for the confusion.

But like exactly what would I put in the script? I’m not too big with Scripting so I don’t really know where to put the stuff.

How do you plan on increasing the value? This should be your question. Once you know how you plan on increasing the value, then come back to the solution I provided.

Where exactly do I put it in the script?

Include it after the variables are set/parented. The changed event allows you to tell whenever the variable changes, then cap it if it is greater than the max value (in this example, 9999)

local eggsopened = Instance.new("IntValue")
	eggsopened.Name = "EggsOpened"
	eggsopened.Value = 0
	eggsopened.Parent = stats

eggsopened.Changed:Connect(function(value)
    if value > 9999  then -- if the "value" of the NumberValue > 9999, then set the NumberValue value to 9999.
       eggsopened.Value = 9999
    end
end)
1 Like

Like that?

Yeah, exactly. You’ll have to add in the function after every new value created, then change the “eggsopened” variable.

Whoops, my bad. Change the

value.Value = 9999

to

eggsopened.Value = 9999