I have a TextBox that needs to be a number.
How can I check if TextBox text type is only numbers? (maybe something like: if string.type(path) == "number"
I have a TextBox that needs to be a number.
How can I check if TextBox text type is only numbers? (maybe something like: if string.type(path) == "number"
function check(var)
if tonumber(var) ~= "" and tonumber(var) ~= nil then
return true
end
end
check(23)
try this
btw this will still return true if there is text and numbers in the text box
Try this:
local textbox = script.Parent.TextBox
textbox.Changed:Connect(function()
local text = tonumber(textbox.Text)
if text ~= nil then
print(text)
end
end)
If the text has any letter added, the if-then statement will prevent whatever you're trying to do from being fired.
To not let them type non-digit characters, you can use the string pattern %D+. It matches 1 or more occurrences of non digit characters.
With that in mind...
textbox.Text = textbox.Text:gsub("%D+", "")
will look for non digit characters and replace them with empty string "".
textbox:GetPropertyChangedSignal("Text"):Connect(function()
textbox.Text = textbox.Text:gsub("%D+", "")
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
Use tonumber https://forum.scriptinghelpers.org/topic/206/how-to-use-tonumber-and-why-it-is-useful/8