Unbinding the render step multiple times

I have a problem with my FPS camera script where, when I unbind the render step, the first time, it for some reason it tries to unbind it 4 times and it doesn’t even unbind it, the second time it tries to unbind it 2 times and it does unbound it and after that it only tries to unbind it only once.

local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local RS = game:GetService("RunService")

local camera = workspace.CurrentCamera

local CameraModule = {}

local character = script.Parent.Parent.Parent.Character

if not character then
	wait()
end

local ShiftLock = require(script.ShiftLock)
local FOVChanger = require(script.FOVChanger)
local NormalCamera = require(script.NormalCamera)
local CameraVarialbles = require(script.CameraVariables)
local FPS = require(script.FPS)

local cameraOffset = CameraVarialbles.cameraOffset
local newOffset = CameraVarialbles.newOffset

FOVChanger.WalkSpeedFOV()
NormalCamera.NormalCamera()

local currentMode = "Normal"
local altToggled = false
local FPSToggled = false

function CameraModule.CameraMode(mode)
	if currentMode == mode then
		return
	end

	if mode == "ShiftLock" then
		ShiftLock.ShiftLock()
	elseif mode == "Normal" then
		NormalCamera.NormalCamera()
	elseif mode == "FPS" then
		FPS.FPSfunction()
	end
	currentMode = mode
end


UIS.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		if currentMode == "Normal" then
			NormalCamera.ReleaseNormCam()
			CameraModule.CameraMode("ShiftLock")
		elseif currentMode == "ShiftLock" then
			ShiftLock.ReleaseShiftLock()
			CameraModule.CameraMode("Normal")
		end
	elseif input.KeyCode == Enum.KeyCode.C then
		if currentMode == "Normal" then
			NormalCamera.ReleaseNormCam()
			CameraModule.CameraMode("FPS")
		elseif currentMode == "FPS" then
			FPS.ReleaseFPS()
			CameraModule.CameraMode("Normal")
		end
	end
end)

return CameraModule

this is the script where I call for the unbinding of the render step

local FPS = {}

local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character

local face = {}

if not character then
	wait()
end

local cameraVariables = require(script.Parent.CameraVariables)

local sensitivity = cameraVariables.sensitivity

local CanViewBody = true

local cameraOffsetVariable = cameraVariables.cameraOffset
cameraOffsetVariable = Vector3.new(0,1,0)

local cameraOffset = cameraOffsetVariable

local function FPSCam()
	camera.CameraType = Enum.CameraType.Custom
	UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

	for _, v in pairs(character:GetChildren())do
		if CanViewBody then
			if v.Name == 'Head' then
				v.LocalTransparencyModifier = 1
				v.CanCollide = false
				if v:IsA'FaceInstance' then
					v.face.LocalTransparencyModifier = 1
				elseif v:IsA'SurfaceAppearance' then
					v.LocalTransparencyModifier = 1
					if v:IsA'FaceControls' then
						v.LocalTransparencyModifier = 1
					end
				end
			end
		else
			if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
				v.LocalTransparencyModifier = 1
				v.CanCollide = false
			end
		end
		if v:IsA'Accessory' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 1
			v:FindFirstChild('Handle').CanCollide = false
		end
		if v:IsA'Hat' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 1
			v:FindFirstChild('Handle').CanCollide = false
		end
	end
	
	local head = character:WaitForChild("Head")
	
	RunService:BindToRenderStep("FPSCamera", Enum.RenderPriority.Camera.Value, function()
		cameraVariables.cameraAngleX -= UIS:GetMouseDelta().X * sensitivity
		cameraVariables.cameraAngleY = math.clamp(cameraVariables.cameraAngleY - UIS:GetMouseDelta().Y * sensitivity, -75, 75)

		local startCFrame = CFrame.new(head.CFrame.Position) * CFrame.Angles(0, math.rad(cameraVariables.cameraAngleX), 0) * CFrame.Angles(math.rad(cameraVariables.cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -100000))

		camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)
		
		character:SetPrimaryPartCFrame(character:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(-UIS:GetMouseDelta().X * sensitivity), 0))
	end)
	
end

CAS:BindAction("FPSCam", FPSCam, false, Enum.KeyCode.C)

function FPS.FPSfunction()
	if character then
		FPSCam()
	else
		player.CharacterAdded:Connect(function(char)
			FPSCam()
		end)
	end
end

function FPS.ReleaseFPS()
	RunService:UnbindFromRenderStep("FPSCamera")
	CAS:UnbindAction("FPSCam")
	
	for _, v in pairs(character:GetChildren())do
		if CanViewBody then
			if v.Name == 'Head' then
				v.LocalTransparencyModifier = 0
				v.CanCollide = true
				if v:IsA'FaceInstance' then
					v.face.LocalTransparencyModifier = 0
				elseif v:IsA'SurfaceAppearance' then
					v.LocalTransparencyModifier = 0
					if v:IsA'FaceControls' then
						v.LocalTransparencyModifier = 0
					end
				end
			end
		else
			if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
				v.LocalTransparencyModifier = 0
				v.CanCollide = true
			end
		end
		if v:IsA'Accessory' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 0
			v:FindFirstChild('Handle').CanCollide = true
		end
		if v:IsA'Hat' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 0
			v:FindFirstChild('Handle').CanCollide = true
		end
	end
end

return FPS

this is the FPS camera

It could be because your script where you use user input service is in Player character scripts. Meaning it recalls them every time so you end up with a bunch of connections when you reset

it just happens the first time I click C, I didn’t try reseting the character yet

Idk what I’ve done but i just swapped the lines where I unbind the context action service and render step and everything suddenly works

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