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

Can I have pointers to string/input allowed characters?

Asked by 9 years ago

For instance, If I have a gui input or text-box where you can enter information in, I only want to take in numbers so how would I do that? What's the method? Can you point me in the right direction?

0
Use the tonumber function for a string (text box text), if the function return nil then the input is not a number. M39a9am3R 3210 — 9y

3 answers

Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

Try this.

text = "abcd1234abcd"
t = {} --Let's store the numbers in that string.

for i = 0, string.len(text) do --Numeric for, counting from 0 to the length of the string.
    local char = string.sub(text, i, i) --Get the current character you're looking at in the string.

    if tonumber(char) then --If the character can be a number value
        table.insert(t, tonumber(char)) --Put the character in the table as a number value
    end
end

print(table.concat(t, ", ")) --Just to try it out.
Ad
Log in to vote
0
Answered by 9 years ago

The easiest method to use for what you want to do is string.gsub.

String.gsub basically scans the string for anything that matches the string pattern given and replaces it with a string specified as an argument. You can find the full list of arguments for string.gsub here.

So, how do I use it?

To use it, you need to know a little bit about string patterns. You can find a list of the common string patterns here. The one you're going to be using is %d, which is the string pattern for a number.

So, let's say you had a script in the textbox and you wanted to check for numbers, you could use the Changed event to fire the function each time the box is typed in and a combination of string.gsub and the %d string pattern which is used for any numerical number.

For example:

local textbox = script.Parent

textbox.Changed:connect(function()
    textbox.Text = textbox.Text:gsub("%d","") --Checks the string for any numbers and replaces it with nothing, essentially removing it.
end)
Log in to vote
-2
Answered by 9 years ago

This is not a request site. you would have to show us what you've tried. but if you really need help, i recommend the ROBLOX forums.

0
I asked for pointers & methods as to how to do it. Arithmeticity 167 — 9y

Answer this question