How can I make this work?

Making a delay changer for something I’m working on and I want it to check what a player typed in a TextBox and if it isn’t a number or if its too small or too big it resets to the default.

changeDelay:GetPropertyChangedSignal("Text"):Connect(function()
	
	local validNumber = tonumber(changeDelay.Text)

	if validNumber then
		if changeDelay.Text > 0.1 and changeDelay.Text < 3 then
			print(validNumber)
		elseif changeDelay.Text < 0.1 and changeDelay.Text > 3 then
			print("Not valid")
		end
	else 
		print("Not valid")	
	end
end)

It currently only prints “Not valid” and never the number.

2 Likes

The issue with your code is that the comparison operators (>, <) are being used on the changeDelay.Text string directly, rather than on the validNumber variable, which is the actual number value obtained from the string using tonumber().

To fix this, you should compare the validNumber variable to the minimum and maximum values you want to allow, rather than comparing the changeDelay.Text string.

Here’s the corrected code:


changeDelay:GetPropertyChangedSignal("Text"):Connect(function()
    local validNumber = tonumber(changeDelay.Text)

    if validNumber then
        if validNumber >= 0.1 and validNumber <= 3 then
            print(validNumber)
        else
            print("Not valid")
        end
    else
        print("Not valid")
    end
end)

This code will now correctly compare the validNumber variable to the minimum value of 0.1 and maximum value of 3. If validNumber is outside this range, it will print “Not valid”. If it’s within the range, it will print the value of validNumber.

However I haven’t scripted in Lua for a while so Lmk if this doesn’t work.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.