How would I make a click to zoom in then zoom out again after a short period of time

I know the title is a little bit weird, but I kind of want to improve on my scripting, and personally I think I can improve step by step. I kind of don’t know how to do something like this and I need some help.

I kind of wanna learn the mechanics used in this game here: Lips Simulator - Roblox

I really hope someone can help me, I think ill be able to make a game out of this. I’ll also mark you as solved if you solve it lol

You can achieve the effect by lerping the Camera’s FOV property. Here’s a quick example.

local function Lerp(a, b, d) return a + (b - a) * d end
local CAMERA = workspace.CurrentCamera

-- change the FOV value based on whether or not something was clicked.
-- i is the current step for the lerp (which has a range from 0 to 1, inclusive. just change that to whatever step you're in within a simple for loop or whatever. 
CAMERA.FieldOfView	= Lerp(CAMERA.FieldOfView, FOV, i)
1 Like

You can achieve this by taking use of TweenService and UserInputService.

In theory you would once right/left mouse button or any other input method is pressed use the method delay(n, function) when the zoom-in has initiated to wait a specific time amount before zooming out again.

Defining constants and variables

local Camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

local ZoomedIn = false

local ZoomInTime, ZoomOutTime = .2, .5
local ZoomInFOV, ZoomOutFOV = 50, 70
local ZoomOutDelay = 2

Zoom in/out function

function CameraZoom(bool)
    local tweenInfo = TweenInfo.new(bool and ZoomInTime or ZoomOutTime)
    local tweenProperties = {FieldOfView = bool and ZoomInFOV or ZoomOutFOV}
    TweenService:Create(Camera, tweenInfo, tweenProperties):Play()
end

Registering user input

function CameraZoomIn() -- additional function so you can use the CameraZoom function to your liking
	if not ZoomedIn then
		ZoomedIn = true
		CameraZoom(true)
		delay(ZoomOutDelay, function()
			ZoomedIn = false
			CameraZoom(false)
		end)
	end
end

UserInputService.InputBegan:Connect(function(input, GameProcessedEvent)
	if GameProcessedEvent then return end -- prevents the input to pass through if typing in a textbox for e.g. a chat
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then -- MouseButton1Down
		CameraZoomIn()
	elseif input.KeyCode == Enum.KeyCode.Q then -- Key down
		CameraZoomIn()
	end
end)

[color=#FFE300] Note: This code is stored in a LocalScript parented to game.StarterPlayer.StarterCharacterScripts. [/color]

1 Like

Do I put everything into one script?

Yes. I just split the various sections of the script to make it easier to understand each individual bit.

1 Like

Thank you so much! It works! :smiley:

Oh, one last question - how would I add a cooldown?

Also, do you think there could be a gamepass for a mega zoom where it zooms in alot? I think it would be really funny!

You can add a cooldown by saving what the tick was when you zoomed in, then afterwards compare the current tick() and the saved tick() to check if the delta of the two ticks is greater than a certain amount.

Add to the variables

local ZoomCooldown = 3
local PrevZoomIn = 0

Modified zoom in/out function

function CameraZoomIn() -- additional function so you can use the CameraZoom function to your liking
    local DeltaTick = tick() - PrevZoomIn
	if not ZoomedIn and DeltaTick >= ZoomCooldown then
	    PrevZoomIn = tick()
		ZoomedIn = true
		CameraZoom(true)
		delay(ZoomOutDelay, function()
			ZoomedIn = false
			CameraZoom(false)
		end)
	end
end
1 Like

I just asked where I was supposed to put it, but I was too fast and didn’t see it said “Add to the variables”. Thanks again!