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

How do I make a number minus limit?

Asked by 5 years ago
Edited 5 years ago

So I am making a script that lets the player minus the numbers but I was wondering how I could make a limit in how far you can minus the number for example 2000 is the smallest number you can get. Subtracting script:

z = script.Parent.Parent.VariableZ
vz = script.Parent.Parent.Variable2

function onButtonClicked()
z.Value = z.Value - 1000
vz.Text="z:("..tostring(z.Value)..")"
if Value = 2000 disable --This is the part that went wrong and what I am you asking to correct
end
script.Parent.MouseButton1Click:connect(onButtonClicked)

Adding script:

z = script.Parent.Parent.VariableZ
vz = script.Parent.Parent.Variable2

function onButtonClicked()
z.Value = z.Value + 1000
vz.Text="z:("..tostring(z.Value)..")"
end
script.Parent.MouseButton1Click:connect(onButtonClicked)

Please Reply

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Math.clamp could be very useful in your situation.

local value = 12
print(math.clamp(value, 20, 50))

The first argument is the input value. The second argument of the clamp function is the minimum number, if the value is smaller than the minimum it will return the minimum value (in this case 20). The third argument is the maximum number, if x is more than the max then the maximum number will be returned.

If you want to have no max limit you can set the third argument to something like this.

local value = 12
print(math.clamp(value, 20, math.huge))

Full script:

z = script.Parent.Parent.VariableZ
vz = script.Parent.Parent.Variable2

function onButtonClicked()
    z.Value = math.clamp(z.Value - 1000, 2000, math.huge)
    vz.Text="z:("..tostring(z.Value)..")"
end

script.Parent.MouseButton1Click:connect(onButtonClicked)
1
It didn't work HiHowAreYah5 19 — 5y
1
DoubleConstrainedValue are now deprecated and math.clamp is the correct way to do this. DanzLua 2879 — 5y
0
I edited the post, it should probably work. Vinceberget 1420 — 5y
Ad

Answer this question