How would i go about changing the character or player's Field Of View with a TextBox?
Thanks for your help
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.
local Player = game:GetService("Players").LocalPlayer local Camera = workspace.CurrentCamera local TextBox = script.Parent TextBox.FocusLost:Connect(function(Return) if not (Return) then return end if (TextBox.Text:match("^%d+$")) then local Field = tonumber(TextBox.Text) Camera.FieldOfView = math.clamp(Field, 70, 120) end end)
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
$, 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:
if (tonumber(TextBox.Text)) then --// Code end
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!