Is it possible to disconnect functions?

Hey there, fellow devs!

So, I’m making a Third Person Shooter game with my friend. Now I’ve a module script called _CameraV2 (Cuz V1 was not working properly). I’ve a local script called Menu inside of the MainMenu gui (ScreenGui). When the deploy button is clicked it fires a remote event and in the server it receives and fires another remote event.

Now I’ve a local script inside StarterGui which is called Client. I receive the 2nd remote event in the client and set the ThirdPersonCameraConnection (A variable) to be the third person function inside the module script. I returned "ThirdPersonCamera" to print it out in the 2nd remote event receiving. It does print it out but it doesn’t disconnect the function.

(If script is needed I’ll send it in the replies.)

Any help would be appreciated, thanks!

  • Arid

What does the actual function contain? Is it a renderstepped connection that runs every frame (typically for most camera modules)? If so then are you making sure to disconnect that?

Otherwise, even if it disconnects you might want to reset the camera (if it’s set to scriptable idk whats in your camera module). Then this post should be helpfull for that purpose of resetting camera back to the player.

Yes it is.

I’ve a local function which is called ReturnToMenu and yes I do disconnect it.

Post doesn’t contain enough Info, you should have posted your script first.

I’ll ask a few more prodding questions in the meantime.

Are there multiple render stepped connections being made?

What happens after you attempt to disconnect the function?

I’ll send a picture and the script hold on a minute.

You could set a variable, then check if it is enabled at the start of the renderstepped. When you want it to no longer run, just change the value of the variable

You could do it something like

local RunService = game:GetService("RunService")

local ThirdPerson = true
RunService.RenderStepped:Connect(function(step)
    if ThirdPerson then
        --code goes here
   end
end)

remoteEvent.OnClientEvent:Connect(function()
    ThirdPerson = false
end
2 Likes

(Removed all scripts for no copying :wink: )

Hmm, never thought of that. I’ll give it a check!

Did my solution work in the desired way?

Hello, you can just use :Disconnect() function.

local OnRenderStepped
OnRenderStepped = RunService.RenderStepped:Connect(function()

end)

RemoteEvent.OnClientEvent:Connect(function()
   OnRenderStepped:Disconnect()
end)
1 Like

Wait a minute you are doing :Disconnect() on a string and not the connection?

you need the store the connection in a variable.

local connection =	RunService.RenderStepped:Connect(function()
end)--stuff in between
return connection 

you sure about that :wink:

Yeah probably. I provided some script, check that. (In Client I disconnect it)

I already did use it, sooooo…

I already checked the module, you are returning a string “ThirdPersonCamera” and not the connection so you are wrong.

Change the module to this to make it work.

--// Module Variables
local _CameraV2 = {}

--// Services
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

--// Player Variables
local Camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

--// Angle(s)
local AngleX = 0
local AngleY = 0
local CameraPosition = Vector3.new(3, 3, 10)

--// Tween(s)
local AimInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.Out
)

--// Others
local IsAiming = false

--// Main
function _CameraV2:ActivateThirdPerson()
	Mouse.Button2Down:Connect(function()
		IsAiming = true
	end)
	
	Mouse.Button2Up:Connect(function()
		IsAiming = false
	end)
	
	local connection = RunService.RenderStepped:Connect(function()
		Camera.CameraType = Enum.CameraType.Scriptable
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		
		if IsAiming then
			TweenService:Create(Camera, AimInfo, {FieldOfView = 45}):Play()
		else
			TweenService:Create(Camera, AimInfo, {FieldOfView = 70}):Play()
		end
		
		if HumanoidRootPart then
			local StartCFrame = CFrame.new((HumanoidRootPart.CFrame.Position + Vector3.new(0, 2, 0))) * CFrame.Angles(0, math.rad(AngleX), 0) * CFrame.Angles(math.rad(AngleY), 0, 0)
			local CameraCFrame = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(CameraPosition.X, CameraPosition.Y, CameraPosition.Z))
			local CameraFocus = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(CameraPosition.X, CameraPosition.Y, -10000))
			
			HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.Position, HumanoidRootPart.CFrame.Position + Vector3.new(Camera.CFrame.LookVector.X, 0, Camera.CFrame.LookVector.Z))
			
			Camera.CFrame = CFrame.new(CameraCFrame.Position, CameraFocus.Position)
		else
			warn("HumanoidRootPart unavailable for " .. Player.Name .. ".")
		end
	end)
	
	ContextActionService:BindAction("CameraMovement", function(_, _, input)
		AngleX -= input.Delta.X * 0.4
		AngleY = math.clamp(AngleY - input.Delta.Y * 0.4, -75, 75)
	end, false, Enum.UserInputType.MouseMovement)
	
	return connection 
end

return _CameraV2

This will make it properly disconnect

2 Likes

Man you got some scripting skills! Thanks a lot as I’ve been struggling with it for about 3 days now, thanks!

1 Like