I’m trying to make a text filter function, and one of the filter options is making a text use only numbers. I want this to also include decimal numbers, but it does not work for some reason. I’ve been stuck on this for a long time now and I can pretty much say that I am stumped.
local function textFilter(text:string, filter:number, min:number, max:number)
if filter == 1 then --filter to only numbers
local filteredText = ""
local decimalCount = 0
for i = 1, #text do
local char = text:sub(i, i)
print(char)
if char:match("%d") then
filteredText = filteredText .. char
elseif char == "." then --the issue is somewhere here
if decimalCount == 0 then
filteredText = filteredText .. char
decimalCount += 1
end
end
end
local number = tonumber(filteredText)
if number then
if min ~= nil and max ~= nil then
number = clamp(number, min, max)
end
text = tostring(number)
print(text)
else
text = ""
end
end
return text
end
Here is the clamp function, I tried making my own in case math.clamp() was messing something up but it made no difference.
local function clamp(x:number,min:number,max:number)
if x < min then
return min
elseif x > max then
return max
end
return x
end
While it works completely fine when putting in numbers, putting in a decimal will not show up and it will get filtered, which is not supposed to happen.
It is absolutely necessary to have decimals because this is what lets the user control the size and position of GUI objects. If you know some stuff about UI, you will know how important that is.
All you need to check if a text can be converted to a number is to use the tonumber function which does support decimals and returns nil if the text isn’t able to be converted to a number
How can I turn this into a proper function? I need to be able to keep the rest of the text (which should be numbers) but only filter out characters I don’t want, such as letters. The only thing I can think of using your idea is to delete the entire text when it isnt a number.
Fixed the issue, I had to rewrite pretty much the whole function.
local function clamp(x:number,min:number,max:number)
if x < min then
return min
elseif x > max then
return max
end
return x
end
local function textFilter(text:string, filter:number, min:number, max:number)
if filter == 1 then --only numbers
local filteredText = ""
local decimalCount = 0
for i = 1, #text do
local char = text:sub(i, i)
print(char)
if char:match("%d") then
filteredText = filteredText .. char
elseif char == '.' then
if decimalCount == 0 then
filteredText = filteredText .. char
decimalCount += 1
end
end
end
if decimalCount == 1 then
if min ~= nil and max ~= nil then
local numberToClamp = tonumber(filteredText:split(".")[1])
if numberToClamp == nil or numberToClamp == "" then numberToClamp = 0 end
local newNumber = clamp(numberToClamp, min, max)
if newNumber == max or newNumber == min then
text = newNumber
else
text = filteredText
end
end
else
local number = tonumber(filteredText)
if number then
if min ~= nil and max ~= nil then
text = clamp(number, min, max)
end
print(text)
else
text = ""
end
end
end
end
Do with the script what you will, I don’t really have time to explain it