Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a number minus limit?

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

✓ Accepted answer

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)

3 comments

1

It didn't work

1

DoubleConstrainedValue are now deprecated and math.clamp is the correct way to do this.

0

I edited the post, it should probably work.

This discussion is preserved as it was. New answers, comments, and votes are closed.