You can write your topic however you want, but you need to answer these questions:
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
What is the issue? Include screenshots / videos if possible!
thats the problem
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
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
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.
Ah, I think you need to change local tarpos = findNearestTorso(character.HumanoidRootPart)
to local tarpos = findNearestTorso(character.HumanoidRootPart.Position)
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
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