Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

0

Use a number value?

1 answer

✓ Accepted answer

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)

2 comments

0

This works, however, it removes the starting text inside the textbox, anyway to fix that?

0

easier way: string.gsub(str,'%D+','')

This discussion is preserved as it was. New answers, comments, and votes are closed.