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

Why won't this work?

Asked by 10 years ago

I have a TextBox, that can be edited. I have a problem though, I want it to be editable, but numbers only. I tried using :Tonumber as suggested by someone (I forgot who), but apparently didn't work...

OUTPUT

Players.Player1.PlayerGui.ScreenGui.GemsToDiamonds.Custom.S:5: attempt to call method 'Tonumber' (a nil value)

What I want is A TextBox be able to type numbers only, then put that number as a Value in TextBox. But is didn't work, someone help please?

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
10 years ago

I think that there is an easier way to check for this, but I can't remember what it is.

string.gmatch

local function is_numbers(txt)
    for i in string.gmatch(txt, "%a") do  -- %a is the patter that looks for any upper or lower case letter.
        -- Immediately return false, if we find a letter.
        return false
    end
    -- If there was no letter, then you're good to go. 
    return true
end



print(is_numbers("99999")) -- Will print true, because there were no letters.
print(is_numbers("stri9ng")) -- Will print false, because there was a letter.


if is_numbers("99999") then 
    print("Everything in the string was a number")
else
    print("There was a letter in the string")
end

--[[ Example ]]--

local txtbox = script.Parent:WaitForChild("TextBox")


local function is_numbers(txt)
    for i in string.gmatch(txt, "%a") do  
        return false
    end
    return true
end

txtbox.Changed:connect(function()
    local store_text = txtbox.Text
    -- Store the current text in a variable, it's just personal preference, it looks cleaner imo. 
    if not is_numbers(store_text) then 
    -- If what you just typed isn't a number then
        txtbox.Text = store_text:sub(1, store_text:len() -1 )
        -- Text = current text - the letter you just typed.
    end
end)


0
Or simply just "return string.match(txt, '%d+') ~= txt" Articulating 1335 — 10y
0
' I think that there is an easier way to check for this, but I can't remember what it is. ' Azarth 3141 — 10y
0
Confusing, Also, I can't see how I can set the number in the textbox as a value (NumberValue) as the Textbox's Child. So I have a textbox, and in it I have a script and the Value. fahmisack123 385 — 10y
Ad

Answer this question