How to make scriptable camera follow player?

Just like the title says, I’m trying to make my scriptable camera follow player but for some reason couldn’t. Here’s the code for my scriptable camera:

local RepStore = game:GetService("ReplicatedStorage")
local ChangePRemote = RepStore.changePerspective
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local function ChangeP(follow)
	print(follow)
	camera.CameraType = Enum.CameraType.Scriptable
	
	local char = player.Character
	local humroot = char:FindFirstChild("HumanoidRootPart")
	local pos = humroot.Position
	
	
	local endpos = pos + Vector3.new(10,50,0)
	player.CameraMinZoomDistance = 25
	camera.CFrame = CFrame.lookAt(endpos, pos)
	camera.CameraSubject = humroot
	
end

ChangePRemote.OnClientEvent:Connect(ChangeP)

What should I add to achieve that goal?

1 Like

FYI, the “follow” argument in the function is unrelated to my question.

if you want the camera to follow the player, Use BindToRenderStep. If it doesn’t work , try to make the CameraType Custom (with BindToRenderStep).

2 Likes

I did it like this:

local RunService = game:GetService("RunService")
local RepStore = game:GetService("ReplicatedStorage")
local ChangePRemote = RepStore.changePerspective
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local function ChangeP(follow)
	print(follow)
	camera.CameraType = Enum.CameraType.Scriptable
	
	local char = player.Character
	local humroot = char:FindFirstChild("HumanoidRootPart")
	local pos = humroot.Position
	
	
	local endpos = pos + Vector3.new(10,50,0)
	player.CameraMinZoomDistance = 25
	camera.CFrame = CFrame.lookAt(endpos, pos)
	camera.CameraSubject = humroot
	
end

local function RunFunc(follow)
	RunService:BindToRenderStep(follow, 1, ChangeP)
end
ChangePRemote.OnClientEvent:Connect(RunFunc)

It works so far, but would this cause performance issues?

As far as you don’t call the remote event more than 1 time, no. If you need the event to be fired multiple times, you could use the UnBinbFromRenderStep, to stop the camera or do whatever you want when you want it to stop, so that the Bind doesn’t repeat more than once and doesn’t cause lag.
Also to reduce lag you could set the variables:

local char = player.Character
local humroot = char:FindFirstChild(“HumanoidRootPart”)

Outside of the function.

2 Likes

this module might help: CameraUtil - Your new tool for simple camera manipulation! (OPEN-SOURCED)

2 Likes

There is a rather simple solution to this.

Instead of touching RunService, you can use the Camera object itself and adjust its CameraType to “Fixed”

It will stop Camera movement and also track the player.

I’ve tried using Fixed camera but it behaved same as Scriptable without the RunService.

i would like to add that this module includes a method which allows you to make the camera follow the player. it’s very easy to settup and works quite well. Highly reccomend if you have to manipulate camera a lot.

I’ll definitely check it out, it sounds quite useful.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.