How to create a zoom effect with a button click

I wanted it so that when the info button was clicked the camera would zoom in

Yes, the code functions like that.

local Camera = workspace.CurrentCamera
local player = game:GetService("Players")
local Mouse = game:GetService("MouseService")
local TweenService = game:GetService("TweenService")
local FOVChange = function()
    script.Parent.Visible = false
    script.Parent.tButton.Visible = false
    script.Parent.Parent.Frame.Visible = true
    return TweenService:Create(Camera, TweenInfo, {FieldOfView = 60})
end

script.Parent.MouseButton1Click:Connect(function(FOVChange)

You can also directly paste this into your script

so do i put this under the mousebutton function?

You can delete your code and paste this on top and it should work


the end line

Can you send the script you ran?

--This the script
local Camera = workspace.CurrentCamera
local player = game:GetService("Players")
local Mouse = game:GetService("MouseService")
local TweenService = game:GetService("TweenService")
local FOVChange = function()
	script.Parent.Visible = false
	script.Parent.tButton.Visible = false
	script.Parent.Parent.Frame.Visible = true
	return TweenService:Create(Camera, TweenInfo, {FieldOfView = 60})
end

script.Parent.MouseButton1Click:Connect(function(FOVChange)

Can you try to remove the return at the function?

Well i think you made a mistake with (function(FOVChange() because the correct way is only this?

script.Parent.MouseButton1Click:Connect(FOVChange)

You wouldn’t need function for that right?

1 Like

i think there is a issue at the return function

I don’t think that return function will give any errors, But i think the tween will not play due to no :Play() thing.

Like: TweenService:Create(C,T,{F = 60}):Play()
The play is missing so it wouldn’t play the tween.

local tweens = game:GetService("TweenService")

local button = script.Parent
local camera = workspace.CurrentCamera

local debounce = false
local toggle = false

button.MouseButton1Click:Connect(function()
	if debounce then
		return
	end
	debounce = true
	
	toggle = not toggle
	local fov = if toggle then 60 else 70
	local tween = tweens:Create(camera, TweenInfo.new(0.2), {FieldOfView = fov})
	tween:Play()
	tween.Completed:Wait()
	debounce = false
end)

Here’s a sample script I just created where when a button is pressed the client’s camera’s field of view is toggled between 60 and 70 (with a tween).

Here’s the model file:
repro.rbxm (3.5 KB)

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

local Button = script.Parent
local OuterFrame = Button.Parent
local InnerFrame = OuterFrame.Frame
local Button2 = Button.tButton

local function FOVChange()
	Button.Visible = false
	Button2.Visible = false
	InnerFrame.Visible = true
	local Tween = TweenService:Create(Camera, TweenInfo.new(0.2), {FieldOfView = 60})
	Tween:Play()
	Tween.Completed:Wait()
end

Button.MouseButton1Click:Connect(FOVChange)

Fixes applied to your script (this is working for me).

1 Like

It will work, The only issue was the

Button.MouseButton1Click:Connect(function(FOVChange)

And it will probably work with the return too but i don’t think that’s the best way to use it.

Well the return wouldn’t work since you still need to play the returned tween object, also “TweenInfo” needed to be “TweenInfo.new()”.

Oof, Didn’t notice that there isn’t TweenInfo Variable for the tween.

Try now, left something in there from your script which I intended to remove.

i fixed it by changing the names, Tysm