How to make tween stop repeating itself?

I made a simple zoom script, that when you hold right click it tweens your fov (creating a zoom effect) I have the basic code down, and it works, but the tween animation keeps resetting and playing again, creating this pumping camera effect when its held down. Is there a way I can make it play once or something?

Here is the code:

--// Services
local UserInputService = game:GetService("UserInputService")
local ts =  game:GetService("TweenService")
local player = game.Players.LocalPlayer
local camera = game.Workspace.Camera


--// Objects

local fovIn = ts:Create(
	camera,
	TweenInfo.new(.5),
	{FieldOfView = 50}
)

local fovOut = ts:Create(
	camera,
	TweenInfo.new(.5),
	{FieldOfView = 70}
)

local Held = false

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
		Held = true
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
		Held = false
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if Held == true then
		fovIn:Play()
	elseif Held == false then
		fovOut:Play()
	end
end)

Are you using TweenInfo? If so TweenInfo contains some values you may need to configure to stop this.

Yes I think, but what would I change to stop it from repeating?

I think the issue is that it’s playing the tween over and over again every single frame

Have you tried replacing Held = true and Held = false with fovIn:Play() and fovOut:Play() instead of doing it in a RenderStepped function?

1 Like

It seems to have fixed it repeating, but now after tweening it goes back to the original fov.

Hmm that’s weird, do you have any other scipts in your game that change the FOV?

Yep, I am an idiot, I left in a audio visualizer script that was constantly resetting the fov to 70. Wow. Its completely fixed now, thanks for the help.

Happens from time to time to all of us :slightly_smiling_face:

Define more properties within TweenInfos or just set the value to the tween objective when the tween is completed

for example:

local fovIn = ts:Create(
   camera,
   TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
   { FieldOfView = 50 }
)

or

fovIn:Play()

fovIn.Completed:Wait()

camera.FieldOfView = 50

In tweeninfo their are 5 values needed, (TweenService | Roblox Creator Documentation )

local tweenInfo = TweenInfo.new(
	2, -- Time it takes for Tween to carry out.
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely) -- This may be the one your looking for. Make it 0 to stop repeating.
	false, -- Reverses (tween will reverse once reaching it's goal) -- this may also be a factor. If this is true you might want to make it false,
	0 -- Time before Tween starts.
)

I put some comments for you to understand the basic functions of TweenInfo

save your script then try this:

local uis = game:GetService("UserInputService") --gets user input service
local ts = game:GetService("TweenService") --gets tween service
local player = game.Players.LocalPlayer --finds the player
local cam = workspace.CurrentCamera --finds the player's cam
local mouse = player:GetMouse() --gets the player's mouse
local ti = TweenInfo.new(0.5) --defines a new tween info with a 0.5 second interval

local fovIn = { --fovIn properties
    FieldOfView = 50
}
local fovOut = { --fovOut properties
    FieldOfView = 70
}

mouse.Button2Down:Connect(function() --right click function
    local tween = ts:Create(cam, ti, fovIn) --creates a tween
    tween:Play() --plays that tween
end

mouse.Button2Up:Connect(function() --right click ended function
    local tween = ts:Create(cam, ti, fovOut) --creates a tween
    tween:Play() --plays that tween
end