TextBox enquiries

I’m trying to program a textbox so that it can do the following (But I have no clue how or what to do.)

1 • The textbox will only allow numbers
2 • The textbox uses it’s input text to change an imagelabel’s image
(I have a rough idea on how to do this but it’s very hacky and the image is usually not the one I was requesting, via the ID)
3 • The textbox can detect if an ImageId is valid or not. This will stop errors being plummeted into the output for every number that is input.

If you have any idea how I can reach my total goal, please help :slight_smile:
Thanks!
~salcret

  1. Unfortunately we don’t have that luxury since there’s no api to detect text while the user is typing. Only thing you can do is just check after the player is done Typing using .FocusLost
  2. Thats as simple as just changing the ImageId property to the Input from the text box
  3. i’d personally check that by evaluating the 1st value returned by the pcall function.
local success, err = pcall(function() imageRequestHere end)
if not success then
    --image id is invalid
end

In the future before posting please have a go at making this system you are trying to make because you never know, you could be able to make the system with no assistance. If you are concerned about your working code then you can always post it in the #help-and-feedback:code-review category.

Firstly we need to detect when the text changes; this can be done by using the GetPropertyChangedSignal() event on the TextBox. This event will fire whenever the text chagnes allowing you to check if the text is a number whenver a new text character has been added. Similarly you could use the FocusLost event on the TextBox to detect when the user has stopped tying and the focus has been lost from the TextBox.

Now we have the event set up we need to check if the text is actually a number. This can be done by using tonumber(). When you use this it will return either the number or nil if the text can’t be transferred to a number. When the text can’t be transferred to a number is when it is a mixture of numbers and types of characters.

Here is a little code sample:

local TextBox = script.Parent

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	if tonumber(TextBox.Text) then
		-- Do something if the text is a number
	else
		-- Do something if the text isn't a number
	end
end)

There isn’t much to explain here so I will give you the code block above but with the relevant edits in to change an ImageLabels image. I can’t guarantee it is the best way to do this though:

local TextBox = script.Parent
local ImageLabel = script.Parent.Parent.ImageLabel -- Added this line

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	if tonumber(TextBox.Text) then
		ImageLabel.Image = tostring("rbxassetid://" .. TextBox.Text) -- Added this line
	else
		-- Do something if the text isn't a number
	end
end)

The only way to detect when something has errored is through pcall().

1 Like

If you want to force the textbox entry to be a valid integer, you can use the following code:

local lastValidEntry

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	local entry = textBox.Text
	
	if entry == "" then
		lastValidEntry = nil
	elseif tonumber(entry) then
		lastValidEntry = math.floor(entry)
	end
	
	textBox.Text = lastValidEntry or ""
end)

If you’re asking how to get from a decal’s ID to an image ID, there’s no proper way to do this. You can set an image’s texture/imageId to https://www.roblox.com/Thumbs/Asset.ashx?width=420&height=420&assetId=DECALIDHERE
Obviously replace DECALIDHERE with the decal ID, and you could check the type of the ID provided using MarketplaceService:GetProductInfo(). AssetTypeId 1 being an image, 13 being decal.

3 Likes

@ObscureBrandon

Yes we do. The Text property of a TextBox changes on the client as they type. You can check for changes to the Text property to determine when a user entered something into the box.


@waterrunner @UsernameMissingOrNil

As opposed to using tonumber (which will just flub the text altogether if the argument can’t be turned into an integer), I suggest you use text box masking. Whenever a text change is implemented, use a string pattern to run over the input text and make sure it’s only comprised of characters you want.

Good tutorial by CM32 on this:


@UsernameMissingOrNil

There is now.

3 Likes

You could also strip anything that isn’t a number from a string using string.gsub.

Example: string.gsub("th1s 15 4 5tr1ng th4t has numb3rs", "[^%d]", "") would return "11545143".

The [^%d] pattern basically means ignore anything ([]) that is not (^) a number (%d). See more on string patterns: Strings | Documentation - Roblox Creator Hub

2 Likes

The code I provided didn’t flub anything, but I agree that CM32’s masking seems like a much nicer method. I’ll probably use that in the future, thanks!

But it still does exactly what the link I supplied does, but in a nicer format. Either way, it’s restricted to the decal’s thumbnail with a maximum 420x420 resolution - not the native resolution (even if it’s capped to 1024x1024). Neither get the actual image, which is a shame, it’s been discussed a lot before but they all result in just deciding to go with the decal’s thumbnail instead.

This is how I would do it. Just every time the textbox changes, it instantly removes any text and only leaves numbers. So fast that we wouldn’t even see the letter.