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()
.