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

How to make a number only textbox like the one in coinflip?

Asked by 7 years ago

So far this is what I have, but I want one where it just doesnt allow letters.

function CheckForNums(Box)
    if Box:IsA("TextBox") then
        TT = {}
        for i=1, #Box.Text do 
            TT[#TT] = Box.Text
        end
        Box.Text = ""
        for _,v in pairs(TT) do
            if tonumber(v) ~= nil then
                Box.Text = Box.Text..v
            end
        end
    end
end

while wait() do CheckForNums(script.Parent) end
0
Use a number value? excellentAnarchy 50 — 7y

1 answer

Log in to vote
2
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

I'd like to know a better way as well~

local textBox = script.Parent:WaitForChild('TextBox')

local function checkstr(str)
    local fullStr = ""
    for i in string.gmatch(str, "%d") do -- Only checks for numbers
        fullStr = fullStr..i
    end
    return fullStr
end

textBox.Changed:connect(function()
    local filter = checkstr(textBox.Text)
    textBox.Text = filter
end)

Or as Disillusions pointed out

local textBox = script.Parent:WaitForChild('TextBox')

textBox.Changed:connect(function(prop)
    if prop == "Text" then 
        textBox.Text = string.gsub(textBox.Text,'%D+','')
    end
end)

0
This works, however, it removes the starting text inside the textbox, anyway to fix that? Ex_plore 62 — 7y
0
easier way: string.gsub(str,'%D+','') Disillusions 61 — 7y
Ad

Answer this question