Well, the following script is basically so that in a text box, you can only put numbers and a certain amount depending on how much value a NumberValue has. I get an error when pressing the Backspace key if there is no number in the textbox, and I don’t know how to fix it
(TextBox)
local selected = script.Parent.Parent.Handler.Selected
local DivideButton = script.Parent.Divide
DivideButton:GetPropertyChangedSignal("Text"):Connect(function()
if selected.Value ~= nil and selected.Value:FindFirstChild("Stack") then
DivideButton.Text = DivideButton.Text:gsub('%D+', '')
DivideButton.Text = math.clamp(DivideButton.Text, 0, selected.Value:FindFirstChild("Stack").Value)
end
end)
I believe you are inputting numbers into the math.clamp() as a string. Even if a string is only numbers it is still a string and not a number value.
To convert it do
local number = tonumber(string)
or vise versa
local string = tostring(number)
In this case, it would return the string “”. Obviously, this isn’t a number, clamp errors. To fix this, I would use tonumber.
local selected = script.Parent.Parent.Handler.Selected
local DivideButton = script.Parent.Divide
DivideButton:GetPropertyChangedSignal("Text"):Connect(function()
if selected.Value ~= nil and selected.Value:FindFirstChild("Stack") then
local num = tonumber(DivideButton.Text:gsub('%D+', ''))
if not num then return end
DivideButton.Text = math.clamp(num, 0, selected.Value:FindFirstChild("Stack").Value)
end
end)
the problem with this is that gsub returns two things(1. the new string, 2. how many times a string was removed)
this means num will always be equal to nil
code that should work
local Selected = script.Parent.Parent.Handler.Selected
local DivideButton = script.Parent.Divide
local Stack = Selected.Value.Stack
DivideButton:GetPropertyChangedSignal("Text"):Connect(function()
local Number = DivideButton.Text:gsub('%D+', '')
if Number ~= "" then
DivideButton.Text = math.clamp(tonumber(Number), 0, Stack.Value)
end
end)