Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Camera not following char, how can I fix?

I am trying to make a camera follow script that follows your humanoid root part. But what happens is that it doesn't even move at all. I want it to follow your character instead. Here's the code:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded
local newpos = workspace.Part.Position
local lookvector = char:WaitForChild("HumanoidRootPart").CFrame.LookVector

game:GetService("RunService").RenderStepped:Connect(function()
	repeat wait()
		workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
	until
	workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable
	workspace.CurrentCamera.CFrame = CFrame.new(newpos - lookvector * 3, newpos + lookvector * 3)
end)

What am I doing wrong here? Help!

2 answers

I hope you find my explanation helpful, here you go.

--[[
  I should note that this will not work if:
  - This is in a ServerScript.
  - This won't work in a ModuleScript unless called by a LocalScript.
]]

coroutine.resume(coroutine.create(function()
    local CurrentCamera = workspace.CurrentCamera
    -- \\ Placed in a thread if you would like to write more below.
    while true do
        game:GetService("RunService").Stepped:Wait()
        if CurrentCamera.CameraType == Enum.CameraType.Scriptable then
            -- \\ Manipulate the camera here.
            local CameraSubject = CurrentCamera.CameraSubject
            local Offset = CFrame.new(0,0,0)
            if CameraSubject:IsA("BasePart") then
                CurrentCamera.CFrame = CameraSubject.CFrame * Offset
            elseif CameraSubject:IsA("Humanoid") then
                -- \\ Humanoid must have a registered RootPart.
                CurrentCamera.CFrame = CameraSubject.RootPart.CFrame * Offset
            end
        end
    end
end))

5 comments

0

@TheLumient Right, but I don't know how to make the camera follow the character while still being in the same position. That's what I'm here for.

0

@Infuriashon, I've edited the code. Let me know if this is what you'd like. It's best to have it go by CameraSubject when making it Scriptable in this case. That would make it much more easily managable.

0

@TheLuminent CameraSubject doesn't work when camera type is set to scriptable. https://developer.roblox.com/en-us/api-reference/property/Camera/CameraSubject

0

Ofcourse it doesn't work but you can set the value, correct?

0

Alongside that, the value can be used to determine where the camera needs to go.

You have an error in line 2, it should be:

local char = plr.Character or plr.CharacterAdded:Wait()

Why do you change camera type in each render? You only need to change it once. Also why the repeat until? It is quite impossible for the property change to fail...

workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:Connect(function()
    workspace.CurrentCamera.CFrame = CFrame.new(newpos - lookvector * 3, newpos + lookvector * 3)
end)

Your question is not very clear, but from the comment you made in @TheLuminent answer, I am assuming you need a stationary camera that follows the character? In this case use:

workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable
local root = char:FindFirstChild("HumanoidRootPart")

game:GetService("RunService").RenderStepped:Connect(function()
    workspace.CurrentCamera.CFrame = CFrame.new(newpos, root.Position)
end)

This script will break, when player resets their character or dies.

This discussion is preserved as it was. New answers, comments, and votes are closed.