Camera only change CFrame once

Hey mate, I’m GFXBIT.
Currently, I’m developing a gun with right-mouse click to aim-down sight.
However, it only change the Camera CFrame once.
if you have any idea how to fix this, it would be great.

Uis.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.MouseButton2 then
		if CanCam == true then
		local cam = game.Workspace:FindFirstChild("Camera")
		cam.CameraType = Enum.CameraType.Scriptable	
		cam.CFrame = Gun.OtherPart.TestSight.CFrame
		CanCam = false
		elseif CanCam == false then
		local cam = game.Workspace:FindFirstChild("Camera")
		cam.CameraType = Enum.CameraType.Custom
		CanCam = true
		end
	end
end)

Video:

How exactly do you want it to work? Walk through it step by step.

1 Like

Hmm…
The camera will always FOLLOW the testSight.Cframe while CanCam is false

You can use RunService’s BindToRenderStep on InputBegan, and UnbindFromRenderStep on InputEnded

Example:

local uis = game:GetService("UserInputService")
local rns = game:GetService("RunService")

local function SightAimRs()
	cam.CFrame = Gun.OtherPart.TestSight.CFrame
end

local function InputBegan(input, gpe)
	if gpe then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		rns:BindToRenderStep("Aim Sight", Enum.RenderPriority.Camera.Value, SightAimRs)
	end
end

local function InputEnded(input, gpe)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		rns:UnbindFromRenderStep("Aim Sight")
	end
end
2 Likes

I’d recommend adding the if gpe then return end line to the “InputEnded” function to prevent its unnecessary execution (when the ended input occurred because the player interacted with a core gui).

That’s exactly why I didn’t include it in InputEnded, because the input can start on a non-core GUI, and end on a core GUI.
In this specific case, the binds would stack.

Example: Hold a mouse button/key, press escape, release it, if you have a gameProcessedEvent check, the InputEnded wouldn’t run.

I might be nitpicking, as everything would be fine if you press the key and release it again, but oh well.

1 Like

On the flip side, if the input began because the player interacted with a core gui but ended with the player not interacting with a core gui then if this line of code was executed rns:UnbindFromRenderStep("Aim Sight") an error would have been thrown/raised and the script would have crashed (as an attempt would have been made to unbind an action which wasn’t currently bound).

https://developer.roblox.com/en-us/api-reference/function/RunService/UnbindFromRenderStep

If there is no bound function by the given name, this method raises an error.

No, we’re not questioning the results, the results change over time because Roblox makes API changes. My post is accurate wrt datastore rate limits.

If the API documentation has issues, please submit threads in #bug-reports:developer-hub so they can be resolved. In my experience the API used to have a lot of inaccuracies when rate limiting was described, but this has been largely resolved now. (cc @UncertainLeo )

Closing this thread since this is veering very off-topic.

1 Like