Touched Camera doesn't work

So basically i’ve made another camera manipulation script. You just walk on a part and your camera is on the position of a part. Here is what i’ve made :


local hit = script.Parent.Touched:Wait()
	local camera = game.Workspace.CurrentCamera

	function UpdateCamera()
		camera.CFrame = game.Workspace.Part1.CFrame
	end

I don’t really get why it doesn’t work. Does anyone know?

it will run once

and you didnt call the fuction yet

also you shouldnt run a function once in this condition

function updateCam()
    cam.CameraType = Enum.CameraType.Scriptable
    cam.CFrame = workspace.Part1.CFrame
end)

You should set its CameraType to scriptable first for it to allow changing its position. Also, I’d recommend using :Connect() instead of :Wait(), because variables can be defined first and the action can be performed and disconnected at any time.

Apart from that, LocalScripts don’t run inside workspace, if you’re using a server-sided script changing the camera’s CFrame will affect the server’s current camera, not the player’s one, so the best thing to do I can think of is just to use a RemoteEvent from server to client.

local hit = script.Parent
local camera = game.Workspace.CurrentCamera

local function UpdateCamera(hit)
	local Character = hit:FindFirstAncestorWhichIsA("Model")
	local Humanoid = Character and Character:FindFirstChild("Humanoid")
	
	if Humanoid then
		local Player = game.Players:GetPlayerFromCharacter(Character)
		
		if Player then
			-- fire remoteevent
		end
	end
end

hit.Touched:Connect(UpdateCamera)
1 Like

Alright I should add a RemoteEvent inside of ReplicatedStorage?

If you want the script to run for everyone that stepped on the part, then yes, and if not, don’t insert it.

You need to. ReplicatedStorage can be accessed from server and client, so that way you can communicate between both of them.

To fire the RemoteEvent you should use FireClient(), and to receive it; OnClientEvent.

Learn more with this article: Remote Functions and Events

RemoteEvent:FireClient(Player, part)
RemoteEvent.OnClientEvent:Connect(function(part)
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = part.CFrame
end)
1 Like

Where do i need to mention Part1?

image
It has red lines and no error in output.

You need to define the camera.

local cam = workspace:WaitForChild("CurrentCamera")

You can only use the CurrentCamera in a Local Script.

So where do i need to place the local script ?

1 Like

1 Like

I’d be preferable you put the script in StarterGui.

Hmmm…

You could try adding task.wait(2) at the very first line of your script or replacing WaitForChild with FindFirstChild.

I think it has nothing with Touched.