Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is wrong with this zoom script?

When you click "z" it is supposed to zoom in This is my first time working with the camera

local zoomMin = 70 -- FOV default
local zoomMax = 40 -- FOV zoom in
local zoomFrames = 10 -- Larger number = slower zoom speed
local zoomWalkSpeedMultiplier = 0.5 -- Slows character down by this factor upon zooming
local Cam = game.Workspace.CurrentCamera
local canZoom = false

	playerPlayer = game.Players.LocalPlayer
	playerCharacter = playerPlayer.Character
	playerHumanoid = playerCharacter.Humanoid

function zoomIn()
--	print[[zoomIn()]]
	isZoomed = true
	playerHumanoid.WalkSpeed = playerHumanoid.WalkSpeed * zoomWalkSpeedMultiplier
	for i = 1, zoomFrames do
		Cam.FieldOfView = Cam.FieldOfView + (zoomMax - Cam.FieldOfView)/3
		wait()
	end
	Cam.FieldOfView = zoomMax
end

function zoomOut()
--	print[[zoomOut()]]
	isZoomed = false
	playerHumanoid.WalkSpeed = playerHumanoid.WalkSpeed
	for i = 1, zoomFrames do
		Cam.FieldOfView = Cam.FieldOfView + (zoomMin - Cam.FieldOfView)/3
		wait()
	end
	Cam.FieldOfView = zoomMin
end

function commandKeys(key)
	if key == "z" and canZoom then
		if not isZoomed then
			zoomIn()
		elseif isZoomed then
			zoomOut()
		end
	end
end

4 comments

0

Probably a sign (arithmetic) error in your for loops.

0

To me, your not calling 'commandKeys' anywhere in the code, so, it's not going to run. :P Ya Silly Billy. :P

0

So just commandkey(key) at the end, right?

0

I assumed you called the function at the end of the script and just didn't include it. Check my answer.

1 answer

To define the function, do

...
-- after you define "playerPlayer"
local mouse = playerPlayer:GetMouse() -- get the player's mouse.
...
-- at the end of the code:

mouse.KeyDown:connect(function(key) -- connect the function
	commandKeys(key) -- call your function, and pass the key argument.
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.