Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I set a max value for a numbervalue?

Asked by 4 years ago

Does anyone know how to set a max value for a number value? I’ve tried a few pieces of code but none of really worked

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

There isn't any property of NumberValue that let's you do this, but remember that there's an event called Changed for this object that fires whenever its value is changed. So with this is mind, simply connect a function to the event that checks if the new value is above the maximum, and if it is, lower it to the max.

local max = 100

local numberValue = Instance.new("NumberValue")
numberValue.Parent = workspace

numberValue.Changed:Connect(function(value)
    if value > max then
        numberValue.Value = max
    end
end)
Ad

Answer this question