Camera focus on a part?

I have no clue what key:byte() does, also I am unfamiliar with Mouse functions as I use the UserInputService, but how I would go about setting this up is doing the following:

local LockOn = Instance.new("BoolValue")

Mouse.KeyDown:Connect(function(key)
   if WhateverYouWant == true then
      LockOn.Value = true
   else
      LockOn.Value = false
   end
end)

local MyFunc
LockOn.Changed:Connect(function()
   if LockOn.Value then
      MyFunc = RS.Stepped:Connect(function()
         --All of your lerping stuff
      end)
   else
      MyFunc:Disconnect()
   end
end)

Key:Byte() allows you to put a number which corresponds to a key, For instance 50 is CRTL.

Mouse is really bad though so im learning to use inputservice.

EDIT: Thanks, I’m gonna use this knowledge and stuff it inside my small brain to hopefully remember it for next time!

In order to do this same thing with UserInputService, you do this:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, GHE)
--there is also an InputEnded if you want to detect when you stop pressing a key
--GHE stands for 'Game Handled Event', this will be true if you press keys during something like typing in chat, where you aren't necessarily doing something ingame

   if GHE then
      return
      --ignore if it's handled by the game
   end

   if Input.KeyCode == Enum.KeyCode.LeftControl then
      --do stuff
   end
end)

OO, it does seem way harder.

Any reasons as why it would be better then Mouse?

It’s not harder, just weird looking at first. I know enough to tell you that in general, UIS is better. I don’t know enough to really tell you why, sadly.

Oh alright, Have you perhaps got discord where you could go more into detail about userinput? Or do you not/do not want to give it, Since the responding time on here of me takes a while.

I honestly don’t really know anything about how different services work or compare to eachother, I just know how to use them to do stuff.

Oh alright then, Kinda sucks my camera is still stuttering, If you wanna know what i mean then join this game, Put your cursor over a NPC and press CTRL.

https://www.roblox.com/games/4138725330/IDK-A-NAME-BOIS-DEV

Could I see your current lerp script? I’ll put it in studio and mess around to see if I can change that twitching.

Alright, Here it is.

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")

local Target
local Locked = false

local LockOn = RS.Stepped:Connect(function()
	if Locked == true then
		wait()
		local LookDirection = CFrame.new(Camera.CFrame.Position, Target.Position)
		local NewCFrame = CFrame.new(Camera.CFrame.Position) * LookDirection
		Camera.CFrame = Camera.CFrame:lerp(NewCFrame, 0.15)
	end
end)

UIS.InputBegan:Connect(function(Input, GHE)
	if GHE then
		return
	end

	if Input.KeyCode == Enum.KeyCode.LeftControl then
		if Locked == false then
			if Mouse.Target.Parent:FindFirstChild("HumanoidRootPart") then
				Locked = true
				Target = Mouse.Target.Parent.HumanoidRootPart
			end
		elseif Locked == true then
			Locked = false
		end
	end
end)

I’ve written the code as I suggested above, and haven’t run into any stuttering issues. Here’s what I have.

local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local Camera = game.Workspace.CurrentCamera
local Mouse = game.Players.LocalPlayer:GetMouse()

local Target
local LookFunc
UIS.InputBegan:Connect(function(Input, GHE)
	if GHE then
		return
	end
	
	if Input.KeyCode == Enum.KeyCode.LeftControl then
		if Target then
			LookFunc:Disconnect()
			Target = nil
		else
			Target = Mouse.Target:FindFirstAncestorOfClass("Model") and Mouse.Target:FindFirstAncestorOfClass("Model"):FindFirstChild("HumanoidRootPart", true)
			if Target then
				LookFunc = RS.Stepped:Connect(function()
					local LookDirection = CFrame.new(Camera.CFrame.Position, Target.Position)
					local NewCFrame = CFrame.new(Camera.CFrame.Position) * LookDirection
					Camera.CFrame = Camera.CFrame:lerp(NewCFrame, 0.15)
				end)
			end
		end
	end
end)
2 Likes

Huh!? Using your code the stuttering is completely gone!
I don’t know what you did or what you changed, But it worked…?

There were a couple things I changed, but one thing that stood out to me was the wait() in your RS.Stepped function. The function already runs on each frame, but you’re telling the script to yield for however long a wait() is (i think 0.05 seconds). I think that would give you 0.05 seconds of free camera movement before it tweens that frame, but that doesn’t explain why it was yanking the camera back to some other position.

Oh yeah i forgot it does each frame so wait() wasnt even needed, Anyways it works so i guess ill take your code and see what might have caused the stuttering.

Thank you so much for helping, This really made me learn way more about cameras!

1 Like