Help for tweening the camera FOV of a vehicle

Hello, I’ve been trying to figure out how I could tween a player’s camera FOV when the speed of their vehicle passes a certain number value, then tween back to the default FOV when the speed is lower than the number value.

So far I have been trying to figure out the logic by printing the output (local script within my vehicle model):

local speedValue = tonumber(gauges.Speed.Text)
if speedValue > 30 then
    wait(1)
    print("tween FOV")
else
    wait(1)
    print("untween FOV")
end

This result of this code is the output being spammed with these print statements nonstop. It works but I can’t seem to figure out how to pass an output ONLY when the value exceeds the target, and again when the value falls under the target.

Also, unsure if I am going about this the best way. If there is a more efficient method for dynamically tweening the FOV of a vehicle, please feel free to let me know.

1 Like

It would be something like:

local TweenService = game:GetService("TweenService")
local tweenThreshold = -- a number
local tweenInfo = -- tween info
local tweenFov = -- fov to tween to
local defaultFov = -- default FOV
local Camera = workspace.CurrentCamera

speedValue.Changed:Connect(function()
    if speedValue >= tweenThreshold and Camera.FOV ~= tweenFov and tweening == false then
        tweening = true
        local tween = TweenService:Create(Camera, tweenInfo, {FieldOfView = tweenFov})
        tween:Play()
        tween.Completed:Wait()
        tweening = false
    elseif Camera.FOV ~= defaultFov and tweening == false then
        tweening = true
        local tween = TweenService:Create(Camera, tweenInfo, {FieldOfView = defaultFov})
        tween:Play()
        tween.Completed:Wait()
        tweening = false
    end
end)
3 Likes

I am getting the following error: " attempt to index number with ‘Changed’ "

Noob question, but how would I convert speedValue for it to work with the Changed function?

Thanks in advance!

Turn it into a NumberValue or IntValue instance and you should be good

Can u give an example of how to turn it to a number or int value? I want to do something similar
To @FreezeShock script.

I was actually just using a variable to hold the number which can’t actually register .Changed events. You need to create an actual Value object, either manually somewhere in the explorer, or by doing:

local NumberValue = Instance.new(“NumberValue”)
NumberValue.Parent = The desired location

You can then replace “speedValue” in the post I marked as a solution with the NumberValue that was just created.

1 Like