How to lower value automatically instead of dynamically?

I made a localscript inside a screengui.
and I want the fov to lower down automatically by the script
instead of just continuing doing this…

  wait(5)

    workspace.Camera.FieldOfView = 69

    wait()

        workspace.Camera.FieldOfView = 68

        wait()

        workspace.Camera.FieldOfView = 67

        wait()

        workspace.Camera.FieldOfView = 66

To do it automatically you will have to use looping or tweening.

Here is an example of a really basic one which can cycle up and down to a goal:

number = 50
waitTime = 0.1
amount = 1

function(goal)
	local step = 0

	--// The entire purpose of this first bit is just to calculate the step in some feasible way.
		-- You could easily achieve the same thing with other maths.
	if goal == number then -- we've done this just because we don't know if the value is there.
		print("No movement needed.")
	elseif goal > number then
		step = amount
	else
		step = -(amount)
	end
	if step then
		repeat
			number = number + step
			wait(waitTime)
			-- You could then set a value to this number.
		until number == goal
	end
end

This is a really basic example and you could shorten / edit a lot of things - but its so you get the idea.

repeat and while loops are your main ones, however you could also use for loops for transition.

If you want to do it on a property with no self written function and for it always to take x amount of time you may want to look into the TweenService. This can be great for changing things like your FOV and its applicable for loads of situations.

1 Like

Pretty simple… Seems like you are a beginner you just need a loop.
Example localscript:

local Camera =  workspace.CurrentCamera -- UseCurrent camera instead of Workspace.camera
while Camera.FieldOfView > 30 do -- Repeat this WHILE FieldOfView is greater then 30 or when keyword break is used
   Camera.FieldOfView = Camera.FieldOfWiew - 1 -- This will set fieldOfView to the same value minus 1
   game:GetService("RunService").RenderStepped:Wait() -- Use this since we are updating the camera
end --End the loop. Everything udnerneath here is run normally when this loop has ended

Also, seems like you mean “How to lower the value Automatically instead of manually”

1 Like

u can achieve this with while loops, check tutorials on yt

It doesn’t seem to be working…

Any errors in the output?
30 C

It says that the script is positive, but it doesn’t do anything

The script is positive? uhmm there is no such thing as a script being positive

oh here’s what it says [ FieldOfWiew is not a valid member of Camera

oh its a typo replace the first W with V

Oh lol didn’t notice that, thank you.

wai wai wai wai wait, forgot something…
I want to stop the fov when it reaches to 30 for example.
how do i do that?

Changed my answer. You can probably also use TweenService as @6Clu siad

can you reply when i mark something as a solution?
btw I’m new to scripting i just started like 3 days ago…
thanks

Yes i can.
I suggest you watch some tutorials. AlvinBlox is pretty good

He also started my scripter career

ok good cuz I thought it’s gonna prevent you from replying aight alls good

Yeah I know, but I feel scripting professionally is too complicated so I feel stressed…

Hi! Sorry for the late reply, But I found a decent way to do this. (Sorry that I’m not doing the regular code format, For some reason it wouldn’t work.)

local stop = false

while true do
wait ()
if stop == false then
workspace.Camera.FieldOfView = workspace.Camera.FieldOfView - 1
if workspace.Camera.FieldOfView < 30 then --Change the 30 to when you want it to stop.
stop = true
end
end
end

what it’s doing here is taking the FOV down by 1 every time, And it always checks if the FOV is less than 30, (Or whatever you change it to) it will change the stop variable to true, Which will stop the code.

The only downsides really to your methods is:

  • It doesn’t incorporate whether the value is above or below.
  • It can’t be recycled easy.

But then it all depends on opinion, tweening however is probably the best method by far.

1 Like