Making a script that checks if textbox's text was changed

Im trying to make a script that would make a grid pop up depending on the number you put in a text box. It would make the grid with colored blocks, but the grid has to have an even number of the colored block. i.e. a 4x4. 4 red, 4 blue, 4 yellow, 4 green. So i need to be able to check if the number is even so I can evenly split the colors between the amount of blocks.

So far i have this

while wait(.1)do
local text = script.Parent.Text
text.Changed:Connect(function()
if text%2 == 0 then
print(ā€œNumber is evenā€)
end
end)
end

I have tried lots of ways but I still cant figure it out.
Any suggestions?

2 Likes

You donā€™t need a loop to check if the Value has changed, it will automatically do that if it does

This code should work

text.Changed:Connect(function()
   local Text = tonumber(script.Parent.Text) -- converts string to number
   if Text % 2 == 0 then
      print"Number is even"
   end
end)

if you want to check for a Specific Property, use GetPropertyChangedSignal

text.GetPropertyChangedSignal("Text"):Connect(function()
   local Text = tonumber(script.Parent.Text)
   if Text % 2 == 0 then
      print"Number is even"
   end
end)

It didnt. I got an error.
Workspace.GridinputPart.SurfaceGui.TextBox.Script:2: attempt to index nil with ā€˜Connectā€™
and second one says this
Workspace.GridinputPart.SurfaceGui.TextBox.Script:3: attempt to call a nil value
The Textbox is on a part. Does that change anything?

Iā€™m not going to fix your issues for you as it is something that you should be able to fix yourself

local text = script.Parent

text.GetPropertyChangedSignal("Text"):Connect(function()
   local Text

   if text.Text ~= "" then
      Text = tonumber(text.Text)
   end

   if Text and Text % 2 == 0 then
      print"Number is even"
   end
end)

You canā€™t use the percent operator on strings. The percent operator is basically the modulo operator, used to get the remainder of a division. As such, it is used on numbers. You can convert a string to a number using the tonumber() function.
while wait(.1) do
local text = script.Parent.Text
text.Changed:Connect(function(newText)
local num = tonumber(newText)

    if(num) then
        if num%2 == 0 then
            print("Number is even")
        end
    end
end)

end