Answered by
5 years ago Edited 5 years ago
You can modify the .FieldOfView
property of Camera
. To inherit this number from a TextBox
, we can tie a listener to the FocusLost
signal. By using it’s EnterPressed
parameter, we can detect when someone presses return—simultaneously escaping the TextBox—and take the Text.
We have to ensure two things: a). That the Text is purely numerical, and that this number remains within a range of 70-120. This is because these are the maximum & minimum FOV degrees that we can set. To do this, we can use math.clamp()
to pressure the number into being in this range.
01 | local Player = game:GetService( "Players" ).LocalPlayer |
02 | local Camera = workspace.CurrentCamera |
04 | local TextBox = script.Parent |
06 | TextBox.FocusLost:Connect( function (Return) |
07 | if not (Return) then return end |
08 | if (TextBox.Text:match( "^%d+$" )) then |
09 | local Field = tonumber (TextBox.Text) |
10 | Camera.FieldOfView = math.clamp(Field, 70 , 120 ) |
If TextBox.Text:match("^%d+$")
confuses you, my apologies. I am simply exercising my string manipulation as I saw this as a good opportunity to do so. For a short explanation, the function is comparing a "pattern" to the string :match()
is being called on. %d+
or "digits" you could say, is what I look for. By using the two anchor points ^
and $
, I say that I want to compare digits from the start to the end of the string.
If that is still difficult, you can simplify the conditional with:
1 | if ( tonumber (TextBox.Text)) then |
This will try to convert the string to a number, if there are any characters beside numerical, this conversion will fail, causing the if-statement to reject it.
If this helps, don’t forget to accept this answer!