How can I make my stats not go negative?

Hello! How can I make this leader stats script not go negative? I already have a max stat value, and I want a Minimum which would be 0, how can I do this?

This is the script

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player

	local coins = Instance.new("NumberValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = stats

	coins.Changed:Connect(function(value)
		if value > 9999999999999999999 then
			coins.Value = 9999999999999999999
		end
	end)

	local money = Instance.new("NumberValue")
	money.Name = "Money"
	money.Value = 0
	money.Parent = stats

	money.Changed:Connect(function(value)
		if value > 99999999999999999999  then
			money.Value = 99999999999999999999
		end
	end)

	local gems = Instance.new("NumberValue")
	gems.Name = "Gems"
	gems.Value = 0
	gems.Parent = stats

	gems.Changed:Connect(function(value)
		if value > 999999999999999999  then
			gems.Value = 999999999999999999
		end
	end)

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

	eggsopened.Changed:Connect(function(value)
		if value > 999999999999999999  then
			eggsopened.Value = 999999999999999999
		end
	end)
end)

Example:
image

Any help will be appreciated!

2 Likes

You already have conditional statements limiting the maximum amount for a value, so just add another conditional statement to limit the minimum amount for a value.

So for example,

		if value > 9999999999999999999 then
			Thing.Value = 9999999999999999999
        elseif value < 0 then
            Thing.Value = 0
		end
2 Likes

math.abs(number) would make any inputted negative number positive.

You could also check if the number is less than 0 before inputting it.

Example:

local InputtedValue = number

if InputtedValue < 0 then -- If the inputted value is negative
    InputtedValue = 0 -- Make the inputted value zero
end
1 Like

Oh, thank you It worked!

I appreciate your help! :slight_smile:

code
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    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

    coins.Changed:Connect(function(value)
        coins.Value = math.clamp(coins.Value, 0, 10^18 - 1)
    end)

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = 0
    money.Parent = stats

    money.Changed:Connect(function(value)
        money.Value = math.clamp(money.Value, 0, 10^18 - 1)
    end)

    local gems = Instance.new("IntValue")
    gems.Name = "Gems"
    gems.Value = 0
    gems.Parent = stats

    gems.Changed:Connect(function(value)
        gems.Value = math.clamp(gems.Value, 0, 10^18 - 1)
    end)

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

    eggsopened.Changed:Connect(function(value)
        eggsopened.Value = math.clamp(eggsopened.Value, 0, 10^18 - 1)
    end)
end)
explanation

you could also use math.clamp(Value, Min, Max)
if the value is below Min it returns Min
if the value is above Max it returns Max
if the value is between the Min and the Max it returns the value
so yea it clamps the value given

also I made it so it uses IntValues so it can hold larger numbers
IntValue limit: 9.2 quintillion or 2^63 - 1
NumberValue limit: ~9 quadrillion or 2^53

also instead of multiple 9’s I used 10^18 - 1 because it is easier to read and it fits inside a IntValue

10^19 - 1 and 10^20 - 1 are too big for IntValues so I didn’t bother using them for coins and money and used 10^18 - 1

last thing I did was make a Players variable because we want to use GetService

sorry if this wasn’t the best explanation, but I hope it helps

1 Like