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)
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
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