How would I make this text not go over 1 but be able to have values like 0.6 or such

Ok so this is the code

Valoare:GetPropertyChangedSignal(“Text”):Connect(function()
Valoare.Text = Valoare.Text:gsub(“%D+%P+”, “”)
if tonumber(Valoare.Text) > 1 then
Valoare.Text = “1”
end
end)

I kind of want it to not be able to go over 1, it works but once i type something like 0.7 it goes back to one because there is a value higher than 1.
How can I fix this?

You can try connecting the TextBox.FocusLost event instead of GetPropertyChangedSignal and see if that works

1 Like

Sadly it quickly changes to 1 right after I click out of the box.

You probably want to use the math.clamp function or if you want to allow negative numbers, the math.min function. You need to convert the text to number, clamp it and then put it back in the text box.

2 Likes
local Min = 0
local Max = 1
local Default = 0

local Safety = true

Valoare:GetPropertyChangedSignal(“Text”):Connect(function()
	if (Safety) then -- Prevent crashing the game.
		local n = tonumber(Valoare.Text) or Default
		Safety = false
		Valoare.Text = math.clamp(n, Min, Max)
		Safety = true
	end
end)
1 Like

Thank you guys!
Didn’t even hear about math.clamp(), until now, haha.