CameraOffset not working without adding wait()

-- Variables

local Enumeration = Enum
local Game = game
local Workspace = workspace
local RunService = Game:GetService("RunService")
local UserInputService = Game:GetService("UserInputService")

-- Camera
local Camera = Workspace.CurrentCamera
local defaultCameraPosition = Camera.CFrame.p

--Character

local player = game.Players.LocalPlayer
local char = player.Character or player.CahracterAdded:Wait()
local head = char:WaitForChild("Head")
local Humanoid = char:WaitForChild("Humanoid")

local Connection

local function OnRenderStep(Input)	
	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, if Input == "Q" then math.pi / 20 elseif Input == "E" then -math.pi / 20 else 0) 
	--WAIT FUNCTION HERE
	Humanoid.CameraOffset = Vector3.new(if Input == "Q" then math.pi/ -2 elseif Input == "E" then math.pi/ 2 else 0,0,0)
end

local function OnInputBegan(InputObject, GameProcessed)
	if GameProcessed then return end
	local Input = InputObject.KeyCode.Name
	if not (Input == "Q" or Input == "E") then return end
	if Connection and Connection.Connected then return end
	Connection = RunService.RenderStepped:Connect(function() OnRenderStep(Input) end)
end

local ray = Ray.new(head.Position, ((head.CFrame + head.CFrame.LookVector * 2) - head.Position).Position.Unit)
local ignoreList = char:GetChildren()
local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

local function OnInputEnded(InputObject, GameProcessed)
	if GameProcessed then return end
	local Input = InputObject.KeyCode.Name
	if not (Input == "Q" or Input == "E") then return end
	if Connection then Connection:Disconnect() end
	Humanoid.CameraOffset = Vector3.new(0, 0, -1)
end

UserInputService.InputBegan:Connect(OnInputBegan)
UserInputService.InputEnded:Connect(OnInputEnded)

This a peek script am working on, (most of this is created by someone in this devforum. I just added the moving it left to right) wont work without adding wait() function.

If I do add the wait function it’s a little glitchy. Does anyone know how to fix this? It works on my game with more scripts and models but not a newer game with nothing but this, This might give you guys a clue on how to solve this. Thanks!

1 Like

just letting you already know, there is a typo. CahracterAdded

3 Likes

right. thanks for pointing that out

When it doesn’t work, is there errors in the output? When it does work, what do you mean by glitchy?
Is the camera CFrame updating properly consistently? I assume only the offset isn’t working as desired?

Nope no errors at all.

The cameraoffset keeps updating and sometimes goes normal and back to the offset set by the script.

I noticed there is cameroffset in the input ended func as well, is either of them working reliably?

The end function works perfectly fine

Based on the observation that adding a wait() makes a difference, I am going to assume that the render priority needs to be higher in order to show after whatever core roblox camera stuff is happening. Try using BindToRenderStep to adjust the render priority, rather than simply connecting it to the RenderStepped event.

Most camera displacements should be done this way since there are often many acting scripts on a character’s camera. It is much like animation priorities in which the animation with the highest priority will play regardless of if there are multiple other animations playing at the same time.

Connection = RunService:BindToRenderStep("CameraOffset", Enum.RenderPriority.Camera.Value + 1, function() OnRenderStep(Input) end)

Looks like by prioritizing the camera created a new problem, You could keep rotating the camera and there is no limit to the rotation (PS : CameraOffset still does not work)