When Calling Connection:Disconnect() The Connection Stays Connected

I’m making a main menu for my game, and I have two functions that are activated by RenderStepped and Stepped that control the CFrame of the camera and parts respectively, but for some reason when I call connection:Disconnect() it doesn’t work at all, and so inbetween task.wait(1), they both continue until the script is destroyed.

image

How can I fix this?

repeat task.wait() until game.Players.LocalPlayer.CharacterAppearanceLoaded

local mainMenu = script.Parent
local menuCamera = workspace.Terrain:WaitForChild("MenuCamera")
local thumbnail = mainMenu:WaitForChild("thumbnail")
local playButton = mainMenu:WaitForChild("playButton")

local camera = workspace.Camera
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

camera.CameraType = Enum.CameraType.Scriptable

function thumbnailTween()
	thumbnail.CFrame = camera.CFrame * CFrame.new(0, 2, -20)
	playButton.CFrame = thumbnail.CFrame * CFrame.new(0, -7, 2)
	print("A")
end

local maxTilt = 20
function cameraTurn()
	camera.CFrame = menuCamera.CFrame * CFrame.Angles(
		math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
		math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
		0
	)
end

game:GetService("RunService").RenderStepped:Connect(cameraTurn)
game:GetService("RunService").Stepped:Connect(thumbnailTween)

camera.CFrame = menuCamera.CFrame

thumbnail.Parent = workspace.Terrain:FindFirstChild("MainMenu")
playButton.Parent = workspace.Terrain:FindFirstChild("MainMenu")

function decalTween(part)
	local tween = game:GetService("TweenService"):Create(part.Decal, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0), {
		Transparency = 1
	})
	
	part:Destroy()
end

mouse.Button1Down:Connect(function()
	print(mouse.Target)
	if mouse.Target == playButton then
		game:GetService("RunService").Stepped:Connect(thumbnailTween):Disconnect()
		game:GetService("RunService").RenderStepped:Connect(cameraTurn):Disconnect()
		
		local headTween = game:GetService("TweenService"):Create(camera, TweenInfo.new(3, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0), {
			CFrame = CFrame.lookAt(game:GetService("Players").LocalPlayer.Character.Head.Position + Vector3.new(0, game.StarterPlayer.CameraMaxZoomDistance / 2, 15), game:GetService("Players").LocalPlayer.Character.Head.Position)
		})
		
		decalTween(playButton)
		decalTween(thumbnail)
		
		headTween:Play()
		headTween.Completed:Wait()
		
		task.wait(1)
		
		camera.CameraType = Enum.CameraType.Custom
		
		thumbnail:Destroy()
		playButton:Destroy()
		script:Destroy()
	end
end)

Nevermind, fixed this by making the connections variables and then disconnecting them directly!

Example for those who need help to this problem

local connection1 = blank:Connect() -- Imagine this as a connection

connection1:Disconnect() -- Disconnects the function

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