Need help on a locking on system

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

  1. What do you want to achieve?
    I would like to make when I clicked the NPC’s torso, my character looks at it.

  2. What is the issue?
    - YouTube
    It seemed to glitch out when I move.

  3. What solutions have you tried so far?
    I tried replacing while true do with while wait(.1) do but it didn’t work.

script:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)

local char = plr.Character
local humrp = char.HumanoidRootPart

local part = script.Parent
while true do

		humrp.CFrame = CFrame.lookAt(humrp.Position, Vector3.new(part.Position.X, humrp.Position.Y, part.Position.Z))

	end
end)

Any help would be appreciated.

I think you accidentally put humrp.Position.Y once instead of part.Position.Y

Its the same, I tried part.Position.Y too and it still doesnt work

edit: it made it even worse with part.Position.Y

Never run while without wait. It will crash the script.

Try to use wait(0.1) maybe it will change something.

still the same, nothing changed. still cant move

Did the script show any error messages?

none, the output doesn’t show any errors

Is it on the server(script) or the client(localscript)?

Its on a server script inside the part

Try looking at this there will be explained how you can do it: CFrames | Roblox Creator Documentation
If it still doesn’t work try to print your variables to see if they exist.

Try

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
   local char = plr.Character or plr.CharacterAdded:Wait()
   local humrp = char.HumanoidRootPart
   local part = script.Parent
   humrp.Anchored = true
   humrp.CFrame = CFrame.lookAt(humrp.Position, Vector3.new(part.Position.X, humrp.Position.Y, part.Position.Z))
end)

and unanchor the humrp when you want it to lock off

That’s not what I wanted.
I want the player to be able to move while locked on a part like in Soulshatters

You might want to see this post and modify your code to fit it (ie. remove the RenderStepped event connection)

Please note that CFrame.new(Vector3, Vector3) is replaced by CFrame.lookAt() (Which you are currently using)

local part = script.Parent

part.ClickDetector.MouseClick:Connect(function(plr)
	local char = plr.Character
	local hmr = char:WaitForChild("HumanoidRootPart")
	while task.wait() do
		hmr.CFrame = CFrame.lookAt(hmr.Position, part.Position)
	end
end)