So I have these two buttons that increase and decrease the number on the screen above. The left button decreases, the right button increases.
However, I only want the NumberValue to go up or down to a certain amount, so it doesn’t exceed the limit. (The minimum being 0, and the maximum being 255.)
function OnClicked()
addremove = game.Workspace.ValuePart.SurfaceGui.Value.Value
game.Workspace.ValuePart.SurfaceGui.Value.Value = addremove + 1
game.Workspace.ValuePart.SurfaceGui.TextLabel.Text = addremove
end
script.Parent.ClickDetector.MouseClick:Connect(OnClicked)
This is the script for the “up” button. The down button is the same except it uses a - instead of a +.
local Gui = game.Workspace.ValuePart.SurfaceGui -- path to your SurfaceGui
function OnClicked()
-- addremove = Gui.Value.Value << (removed this line, changed the one below)
Gui.Value.Value = math.clamp(Gui.Value.Value + 1, 0, 255)
Gui.TextLabel.Text = Gui.Value.Value
end
script.Parent.ClickDetector.MouseClick:Connect(OnClicked)
The math.clamp() function was specifically made to replace constrained value objects.