How would I change this so that the function is connected by default?

Hi there, this may seem like a simple topic but the scripting isn’t as I can’t wrap my head around the variables, etc. I’m trying to change the function so that it is connected by default. I’ve tried changing the if not connected then to if connected then however it doesn’t change it. Basically, my script is a FOV visualizer script that connects when a gui button is clicked. When it is clicked again while being connected, it disconnects and vice versa.

Script below:

local RunService = game:GetService("RunService")
local Button = script.Parent
local musicplayer = workspace.Sound
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 70,
	CameraMaxFOV = 80,
	CameraMaxVolume = 1500
}

local connected = false

local function changeCameraShakeState()
	if not connected then
		game:GetService("RunService"):BindToRenderStep("CurrentTrack", 1, function()
			local CurrentLoudness = musicplayer.PlaybackLoudness
			local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
			if FOV > 0 and FOV < 130 then
				CurrentCamera.FieldOfView = FOV
			end
		end)
	else
		RunService:UnbindFromRenderStep("CurrentTrack")
	end
	connected = not connected
end

Button.MouseButton1Click:Connect(changeCameraShakeState)
2 Likes
CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(changeCameraShakeState)

This will connect the callback whenever the camera is moved, not sure what you’re trying to ask for.

1 Like

I’ll send you a video on what I mean.

In the video, you see that I click the camera button to toggle the FOV effect and then click it again to untoggle it. You’ll also see that it isn’t toggled by default. What I want is for that effect to be toggled on by default when you join the server.

Sorry if it is hard to see the effect (the intensity is just low).

Ignore the lighting and focus on the effect being toggled on and off.

Found the solution. Made another script with the same camerafov code and then changed the if not connected then to if connected then in the old script.