I need help with a zoom out script

so basicaly how i want for this script to worki is when player pressed the keybind it will zoom out players camera stay like this for a second and then zoom by itself back in but it doesnt work as i want it to can someone please help me

local player = game.Players.LocalPlayer
local zoomOutDistance = 100
local zoomInDistance = 10
local zoomDuration = 1
local UserInputService = game:GetService("UserInputService")
local keyToZoom = Enum.KeyCode.Z 

local function zoomCamera()
	player.CameraMaxZoomDistance = zoomOutDistance
	player.CameraMinZoomDistance = zoomOutDistance
	wait(zoomDuration)
	player.CameraMaxZoomDistance = zoomInDistance
	player.CameraMinZoomDistance = zoomInDistance
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == keyToZoom and not gameProcessed then
		zoomCamera()
	end
end)

1 Like

From what it looks like, it should work if it’s in a localscript rather than a normal script (assuming its that).

You aren’t targetting the camera, so you should do that too while you’re there.

that said, you should also add a debounce / cooldown so it won’t be spammed and replace wait() with task.wait() so it’ll use task library rather than the deprecated function

I think the problem might be the camera zoom distance might not be fully refreshing, leading to no change. The script I will provide below might work, but it’s possible it won’t.

-- Changing zoom distance here

local camera = workspace.CurrentCamera
local position = camera.CFrame.Position - camera.Subject.Position -- Gets the offset from the camera subject

position *= zoomOutDistance / zoomInDistance -- Changes the position to a new offset

camera.CFrame = camera.CameraSubject.CFrame.Position + offset -- Converts the new offset back into a CFrame

task.wait(zoomDuration) -- Wait the zoom duration using task.wait() rather than wait() as wait() is less accurate

-- Changing zoom distance again here

position = camera.CFrame.Position - camera.Subject.Position -- Refresh the position variable to what it was before

position *= zoomInDistance / zoomOutDistance -- Changes the position back to what it was before offset

camera.CFrame = camera.CameraSubject.CFrame.Position + offset -- Converts the new offset back into a CFrame

I hope this helps!

Maybe use tweening/interpolating to move the camera, then use Zoom Distance to make sure the player can’t change it unless you want them to. By the way, Interpolating is decaperated

hey so basicaly how i want this to work when player clicked the keybind it will zoom in also with like a transtion and stay zoomed in for 2 seconds and zoom back in because right now it works like it zooms in without a transition and doesnt zoom back in by itself after a second

TweenService is the service you want to use.

local player = game.Players.LocalPlayer
local zoomOutDistance = 100
local zoomInDistance = 10
local zoomDuration = 1
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local keyToZoom = Enum.KeyCode.Z


local function zoomCamera()
	TweenService:Create(player,TweenInfo.new(0.5),{CameraMaxZoomDistance = zoomOutDistance})
	TweenService:Create(player,TweenInfo.new(0.5),{CameraMinZoomDistance = zoomOutDistance})
	wait(zoomDuration)
	TweenService:Create(player,TweenInfo.new(0.5),{CameraMaxZoomDistance = zoomInDistance})
	TweenService:Create(player,TweenInfo.new(0.5),{CameraMinZoomDistance = zoomInDistance})
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == keyToZoom and not gameProcessed then
		zoomCamera()
	end
end)
1 Like

Utilizing TweenService as indicated in the previous post can effectively achieve that; I will supply a modified version.

-- In the start
local tweenService = game:GetService("TweenService")

local info = TweenInfo.new(
	0.5, -- The duration of the transition (you can change this as you please)
	Enum.EasingStyle.Sine, -- You can change this to fit your needs, this starts slow and ends fast or the opposite
	Enum.EasingDirection.InOut, -- You can change this to what you want. InOut means it starts with the start of the tweenType and ends with a reversed version of the start.
	0,
	false,
	0
)
local debounce = false -- Declare a debounce so people can't zoom infinitely out

local zoomDuration = 1 -- Change this to however long you want it to stay zoomed out

-- Makes sure the function cannot be called while it is running
if debounce == false then
debounce = true 

-- Changing zoom distance here
local camera = workspace.CurrentCamera
local position = camera.CFrame.Position - camera.Subject.Position -- Gets the offset from the camera subject

position *= zoomOutDistance / zoomInDistance -- Changes the position to a new offset

-- Old version setting the CFrame
camera.CFrame = camera.CameraSubject.CFrame.Position + offset

-- New version using tweenService
local tween = tweenService:Create(camera,info,{CFrame = camera.CameraSubject.CFrame.Position + offset})

tween:Play() -- Play the tween as it is now referenced

tween.Completed:Connect(function()
task.wait(zoomDuration) -- Wait the zoom duration until it's time to change the position back

-- Changing zoom distance again here

position = camera.CFrame.Position - camera.Subject.Position -- Refresh the position variable to what it was before

position *= zoomInDistance / zoomOutDistance -- Changes the position back to what it was before offset

local tween2 = tweenService:Create(camera, info, {CFrame = camera.CameraSubject.CFrame.Position + offset})
tween2:Play()

tween2.Completed:Connect(function()
debounce = false
end)
end)
end

Please note that if the player moves during the transition, it may cause glitches as TweenService is not designed for moving parts in most scenarios. However, setting the WalkSpeed to 0 or anchoring the HumanoidRootPart can resolve this issue. (Anchoring if changing the WalkSpeed doesn’t work.) I’ve also added a debounce so players cannot zoom infinitely out or stay zoomed out if they want. (You can also use task.wait() after debounce = false to keep the player from zooming out again for a few more seconds.)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.