Trying to make a script that sets the player's camera to a part, but it doesn't always work

I have a script that’s supposed to set the player’s camera to the CFrame of a part named CameraPart, but it only does it a percentage of the time.

This is the LocalScript I’m using:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

repeat wait()
	Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = workspace.CameraPart.CFrame

while true do
	Camera.CFrame = workspace.CameraPart.CFrame
	task.wait(0)
end

I don’t know why it only works some of the time. My only guess would be to use something like WaitForChild, but I’m not an expert scripter. The reason I have it so the script constantly sets the player’s camera to the CFrame of the part instead of once is because the part moves.

1 Like

use RunService.RenderStepped instead of a while loop and get rid of the pointless task.wait(0)

Your code fixed:

local Camera = workspace.CurrentCamera
local part = workspace.somepart
Camera.CameraType = Enum.CameraType.Scriptable
local run = game:GetService("RunService")

run.RenderStepped:Connect(function()
   Camera.CFrame = part.CFrame
end)


Edit: The other guy just reuploads the same code with small changes that don’t change anything and he gets solution? Whatever

1 Like

Try this :

local cam = workspace.CurrentCamera
local camPart = workspace:WaitForChild("CameraPart")

cam.CameraType = Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:Connect(function(dt)
  cam.CFrame = camPart.CFrame
end)
1 Like

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