How do I change CurrentCamera CFrame on part touch?

My intention is to make the camera’s CFrame when the player is touching a brick. I’ve used this local script below:

local player = game.Players.LocalPlayer
local db = false
local cam = workspace.CurrentCamera
local campart = workspace.Cam

script.Parent.Touched:Connect(function(hit)
	if not db then
		db = true
		if hit.Parent:FindFirstChild("Humanoid") then
			cam.CameraType = Enum.CameraType.Scriptable
			cam.CFrame = campart.CFrame
		end
	end
end)

but it doesn’t work. There’s no output or any errors whatsoever.

There’s no errors in output because your using a local script for using touched functions.

Well there’s no output either if i use a normal script

Probably because you added the camera in a normal script, I don’t think it works on normal scripts.

do I have to place the local script elsewhere?

For now, delete the camera scripts and add it to a different script. After that, add prints to check if it even works.

If it prints what you wanted, it works. From there I will think of something.

The only way I know how to change the camera’s CFrame is by placing a script in the brick that activates a local script in StarterGui. However, that seems quite troublesome rather than having just 1 script in my opinion.

That’s probably the only way sadly, since you can’t send events from a normal script to a Local Script.

This is very wrong, you can easily send events from a serverscript to a local script.

:FireClient()

is the function.

And yeah, you can’t put a local script in the workspace in that manner.

You’ll need to put a serverscript that detects the touch then fires the client and then in a local script, have the camera CFrame change

Yeah, you would need to access the client’s CurrentCamera by a LocalScript and handle the onTouch event on the server.

LocalScript; place in StarterCharacterScripts or StarterPack

local ReplicatedStorage = game:GetService("ReplicatedStorage");

local SetCameraType = ReplicatedStorage:WaitForChild("SetCameraType");
local CurrentCamera = workspace.CurrentCamera;

local changedCamera = false;

if not changedCamera then
	SetCameraType.OnClientEvent:Connect(function(cameraPart)
		CurrentCamera.CameraType = Enum.CameraType.Scriptable;
		CurrentCamera.CFrame = cameraPart.CFrame;
	end)
end

ServerScript; Placed within the touched part.

local Players = game:GetService("Players");
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local part = script.Parent;

local cameraPart = workspace:FindFirstChild("CameraPart");

local setCameraType = Instance.new("RemoteEvent");
setCameraType.Name = "SetCameraType";
setCameraType.Parent = ReplicatedStorage;

part.Touched:Connect(function(hit)
	local Character = hit.Parent;
	local Humanoid = Character:FindFirstChild("Humanoid");
	if Humanoid then
		local Player = Players:GetPlayerFromCharacter(Character);
		setCameraType:FireClient(Player, cameraPart);
	end
end)
3 Likes

Don’t be do client stuffs on here, just use the argument from Touched event and refer the player’s CurrentCamera instead.

1 Like