Camera script works on studio but not on client

I’m using a over the shoulder script made by B Ricey on YouTube, the problem is when I use it in studio it works perfectly, but when I use it in the Roblox client or in game its won’t work at all and my camera is stuck (doesn’t follow player).

local Workspace = game:WaitForChild("Workspace")
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local camera = Workspace.CurrentCamera
local player = Players.LocalPlayer
local cameraOffset = Vector3.new(2, 2, 8)

player.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")

	local cameraAngleX = 0
	local cameraAngleY = 0

	humanoid.AutoRotate = false

	local function playerInput(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Change then
			cameraAngleX -= inputObject.Delta.X
			cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)
		end
	end
	ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

	RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
		local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame =  startCFrame:PointToWorldSpace(cameraOffset)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -100000))

		camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)

		local lookingCFrame = CFrame.lookAt(rootPart.Position, camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -100000)))

		rootPart.CFrame = CFrame.fromMatrix(rootPart.Position, lookingCFrame.XVector, rootPart.CFrame.YVector)

	end)

end)

local function focusControl(actionName, inputState, inputObject)
	if inputState == 	Enum.UserInputState.Begin then
		camera.CameraType = Enum.CameraType.Scriptable

		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		UserInputService.MouseIconEnabled = true

		ContextActionService:UnbindAction("FocusControl")
	end
end

ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

This explains what you have to do to properly connect PlayerAdded and CharacterAdded:
CharacterAdded functions not loading correctly - Help and Feedback / Scripting Support - Developer Forum | Roblox

In a nutshell, you’re not handling the case for when your player’s character already exists upon CharacterAdded being connected.

1 Like

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