How do I make it so that if the function is disconnected and is clicked again, it connects the function, vice versa?

Hi, sorry if I’m constantly posting but I’m in need for some help in different topics. I have a function script inside a text button that disconnects the function after you click it, but the problem is, is that it doesn’t connect back after it’s clicked again? How do I solve this and what would a example be of this script?

local RunService = game:GetService("RunService")
local Button = script.Parent

local function UnbindCameraShake()
	RunService:UnbindFromRenderStep("CurrentTrack")
end

Button.MouseButton1Click:Connect(UnbindCameraShake)

Create a variable that determines if the function is connected or not, connect/disconnect depending on that variable and invert the variable’s boolean

local RunService = game:GetService("RunService")
local Button = script.Parent

local connected = false

local function changeCameraShakeState()
	if not connected then
		--Render Step code to connect here
	else
		RunService:UnbindFromRenderStep("CurrentTrack")
	end
	connected = not connected
end

Button.MouseButton1Click:Connect(changeCameraShakeState)

would the renderstep code be the fov script in this case?

The render stepped code would be the BindToRenderStep code

as in BindToRenderStep(“CurrentTrack”)?

Yep, and of course the other stuff connected to that BindToRenderStep, I recall you had this before in a previous post

You mean this?

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)

Yep, you put that after the if not connected then line

quick question, since this is in another script, do I have to type down the locals for the musicplayer in the function?

Yep, or you can try to include both scripts in 1 if possible

I haven’t tried this but is this correct?

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)

I believe so, try it out and see if you get any errors

thanks i found the solution, it works now

1 Like