Camera Manipulation

Hello developers! :wink:

I have this script that when it is clicked, it changes between a part CFrame and the CurrentCamera (Origin) of the player.

So there is a problem, the script works ONCE and then stops working.

I’ve already tried using a normal Script in StarterGUI and Workspace, I’ve tried using a LocalScript in StarterGUI and Workspace, I’ve tried leaving the Script and LocalScript inside the model, I’ve tried all the available options to try to fix the problem, and I haven’t been able to. resolve it.

I even resorted to Artificial Intelligence, but even it was unable to solve the problem.

I’m desperate to fix this problem, and I hope you can help me.

Thank you in advance for your attention, have a great day! :slight_smile:

The LocalScript is located at StarterGUI

local camera = workspace.CurrentCamera
local camLocked = false
local originalCFrame = camera.CFrame

-- Alternating cameras
local function ToggleCameraLock()
	if not camLocked then
		-- Lock camera pos at Cam1
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = workspace.ZoomSystem.Cam1.CFrame
		camLocked = true
	else
		-- Unlock camera and restore Original CFrame
		camera.CameraType = Enum.CameraType.Custom
		camera.CFrame = originalCFrame
		camLocked = false
	end
end

-- Connect MouseClick event to ToggleCameraLock function
local clickDetector = workspace.ZoomSystem:FindFirstChild("ClickZoom") and workspace.ZoomSystem.ClickZoom:FindFirstChild("ClickDetector")
if clickDetector then
	clickDetector.MouseClick:Connect(ToggleCameraLock)
else
	warn("Click detector not found!")
end
3 Likes

maybe try binding it to renderstep? RunService:BindToRenderStep() or use RunService.RenderStepped

1 Like

After setting camera back to custom, you also need to set the CameraSubject back to player’s humanoid. There is no need to set the CFrame, as this will be automatically adjusted with the internal camera script.

-- Unlock camera and restore Original CFrame
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
--camera.CFrame = originalCFrame
camLocked = false
1 Like

If you try zoom camera to the part then

local cam=workspace.CurrentCamera
cam.CameraSubject=workspace:WaitForChild('part')

If you want glue camera to the part

local render = game["Run Service"].RenderStepped
local cam=workspace.CurrentCamera

render:Connect(function()		
		cam.CFrame=workspace.part.CFrame		
end)
1 Like

None of your suggestions work, sorry for wasting your time :frowning:

Changing the subject, I got scripts that can manipulate the camera! But the script that enables / disables it is not able to ā€œfindā€ (I think) the scripts inside the player.

StarterPack
image

Player inventory (on game)
image

Enable / Disable LocalScript

local clickDetector = script.Parent
local lockScript = game.Players.LocalPlayer.Backpack.Lock
local unlockScript = game.Players.LocalPlayer.Backpack.Unlock
local Locked = script.Parent.Parent.Locked

Locked = false

clickDetector.MouseClick:Connect(function()
	if Locked == false and lockScript.Disabled == true then
		Locked = true
		lockScript.Disabled = false
		unlockScript.Disabled = true
	elseif Locked == true and lockScript.Disabled == false then
		Locked = false
		lockScript.Disabled = true
		unlockScript.Disabled = false
	end
end)

I need LocalScript to be able to enable / disable scripts
I hope you can help me! Thanks :slight_smile:

1 Like

ai is never the answer

try putting your client scripts in StarterPlayer>StarterPlayerScripts or ReplicatedFirst, and adding some :WaitForChild()s on the

local clickDetector = workspace.ZoomSystem:FindFirstChild("ClickZoom") and workspace.ZoomSystem.ClickZoom:FindFirstChild("ClickDetector")

line

its also probably good practice to store them in those places if they’re client scripts that run on execution

Thank you, I’ll test them right now.

AWNSER: Didn’t worked

Lock and Unlock scripts don’t work outside of the player’s backpack.

1 Like

Why you just don’t turn off or turn on script when u need change the camera? Do you try change camera subject or set camera CFrame same as target CFrame?

if you are using a local script, make sure to use :WaitForChild() because local script run usually fast before things can be loaded

Disabling and re-enabling scripts is generally a bad practice. For example, disabling a script, WILL NOT disconnect connections that this script has made.

Which may be a reason that your script does not work. After enabling Lock script, you now how 2 active connections and they might be both toggling the the camera, leaving the result the same.

One final check, trivial I know, but are you sure that the ClickDetector is within reach and view after the camera switch? That is, does the mouse cursor changes to the ā€œPoinging fingerā€ icon? If not, you may want to increase the MaxActivationDistance property.

Then why disabled script disabling functions?