Player/Mob Lock On System

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a player/mob lock on system that looks with the body of the character and the camera to the nearest mob/player
  2. What is the issue? Include screenshots / videos if possible!
    nilval
    thats the problem
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried with that what I wrote in a localscript
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local Focus = false

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F then
		Focus = not Focus
		if not Focus then
			game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
		else
			game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		end
	end
end)

while game:GetService("RunService").RenderStepped:Wait() do
	if Focus then
		local charpos = character.HumanoidRootPart.Position
		local tarpos = findNearestTorso(character.HumanoidRootPart)
		local mixpos = Vector3.new(tarpos.Position.X,charpos.Y,tarpos.Position.Z)
		local newcframe = CFrame.new(charpos,mixpos)
		character:SetPrimaryPartCFrame(newcframe)
		game.Workspace.CurrentCamera.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(5,3,10))
	end
end

function findNearestTorso(pos)
	local distance = 100
	local part = nil
	for _,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v:FindFirstChild("Humanoid").Health > 0 then
			if (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude < distance then
				part = v:FindFirstchild("HumanoidRootPart")
				distance = (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude
			end
		end
	end
	return part
end
6 Likes

So looking at line 21 we see

local tarpos = findNearestTorso(character.HumanoidRootPart)

The error code you’re getting shows that you’re getting a nil value from findNearestTorso, which makes sense as you do

local part = nil

and then you have only one conditional in which you set it as not nil.

My assumption would be that you need to check if findNearestTorso returns nil and do what you need with that information.

added this to the script but is not working

function findNearestTorso(pos)
	local distance = 100
	for _,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v:FindFirstChild("Humanoid").Health > 0 then
			if (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude < distance then
				part = v:FindFirstchild("HumanoidRootPart")
				distance = (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude
			end
		end
	end
	if part ~= nil then
		return part
	end
end

at the end

Yes, because you’re still returning a nil part.

What do you expect to happen when a torso is not found within the 100 stud radius?

You need to incorporate the answer to that in the appropriate place in your script. My guess would be after local tarpos = findNearestTorso(character.HumanoidRootPart) include if tarpos == nil then return end, which will prevent the script from running any further if a torso has not been found.

1 Like

problem is idk where to put and im in a radius from a torso

if tarpos == nil then return end
2 Likes

Ah, I think you need to change
local tarpos = findNearestTorso(character.HumanoidRootPart)
to
local tarpos = findNearestTorso(character.HumanoidRootPart.Position)

1 Like
while game:GetService("RunService").RenderStepped:Wait() do
	if Focus then
		local tarpos = findNearestTorso(character.HumanoidRootPart.Position)
		local charpos = character.HumanoidRootPart.Position
		local mixpos = Vector3.new(tarpos.Position.X,charpos.Y,tarpos.Position.Z)
		local newcframe = CFrame.new(charpos,mixpos)
		character:SetPrimaryPartCFrame(newcframe)
		game.Workspace.CurrentCamera.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(5,3,10))
	end
end

function findNearestTorso(pos)
	local distance = 100
	local part = nil
	for _,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v:FindFirstChild("Humanoid").Health > 0 then
			if (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude < distance then
				part = v:FindFirstchild("HumanoidRootPart")
				distance = (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude
			end
		end
	end
	return part
end

returns the same error on line 20 aka local tarpos
im testing it on dummy’s

1 Like

Do the dummy have Humanoid inside them?

1 Like

yes they have humanoid in them

1 Like

Maybe remove the local modifiers from distance and part as shown here. I don’t know if it will wokr but worth a shot

function findNearestTorso(pos)
	distance = 100
	part = nil
	for _,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v:FindFirstChild("Humanoid").Health > 0 then
			if (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude < distance then
				part = v:FindFirstchild("HumanoidRootPart")
				distance = (v:FindFirstChild("HumanoidRootPart").Position - pos).Magnitude
			end
		end
	end
	return part
end
1 Like

nope don’t work what is this that nobody can find out

1 Like

i made some test with print and yet i know that the error is just this 1 line with tarpos it doesn’t even execute findNearestTorso

1 Like