Locking player camera onto a moving part

Hello! I have this script and i’m wondering how I would lock the camera onto a moving part. I tried unanchoring the part that the camera goes to, and welding it to something else but that didn’t work as it should.

    local TweenService = game:GetService("TweenService")

local camera = game.Workspace.Camera

local cutsceneTime = 6

local tweenInfo = TweenInfo.new(
cutsceneTime,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out,
0,
false,
0
)

function tween(part1,part2)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = part1.CFrame

local tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
tween:Play()

wait(cutsceneTime)

camera.CameraType = Enum.CameraType.Custom
end

game.ReplicatedStorage.Cutscene.OnClientEvent:Connect(function()

tween(game.Workspace.Test1, game.Workspace.Test2)
end)

Easy solution to this. For line 3, get the CurrentCamera instead of the camera.

local camera = game.Workspace.CurrentCamera

1 Like

That unfortunately didn’t change anything.

While I’m going to try to find the solution for this script, are there any errors in the output?

The script works for me and it’s because you forgot to add the argument in your server script. (which is player). If it still doesn’t work, replace Camera with CurrentCamera.

event:FireClient(player)

I have a part which you touch that fires it for all the clients. I tried replacing game.Camera with game.CurrentCamera on line 3 but that didn’t have any effect.

You can probably use runservice.renderstepped (is an event) and then set workspace.currucentcamera to the position of the part. (Everything you put inside de renderstepped function will fire before the player’s frame updates (without fps unlockers but normal max fps 60 times/second)

Alright, so this is a pretty simple solution. All you have to do is set the camera subject to the part instance as follows:

local camera = workspace.CurrentCamera -- you should always retrieve the users camera using workspace.CurrentCamera instead of workspace.Camera
camera.CameraSubject = part1

However, I must ask, are you scripting this in a local script or a server script? If your doing it in a server script you should instead script this in in a local script. You should edit the client from a local script if the change should only effect the client.

Tell me if this doesn’t work for you and I’ll try to help further

1 Like
local camera = game.Workspace.CurrentCamera
local part = workspace.Part

camera.CameraSubject = part

Yeah you jsut do this

Yes, this is in a local script. It is in the StarterGui aswell.

Did my code work for you or do you want it so it tweens to the part and then is the camera subject?

Why do you keep using the code I write and then responding to me? First it was in this topic I created. Now this. I noticed this time that you actually changed it a bit though, which was unnecessary since the part didn’t need to be defined because it was already defined somewhere in his code. However, that’s besides the point.

To me it seems like you are trying to attract my attention. Congratulations, you have it. Let’s celebrate this occasion with you getting to the point of why you are doing so.

I’m sort of confused on how to implement it. I’m guessing i need to do something on lines 3, and 18.