Why does the humanoid work incorrectly?


Here is a code snippet:

	if target then
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {target}
		local targetPosition = target.HumanoidRootPart.Position + target.HumanoidRootPart.CFrame.LookVector * 13
		local raycast = workspace:Raycast(targetPosition, Vector3.new(0, -1000, 0), raycastParams)
		if raycast then
			script.Parent.HumanoidRootPart.Position = targetPosition
		end
		script.Parent.Humanoid:MoveTo(target.HumanoidRootPart.Position)
	end
	wait(1)
	if target then
		local nearestPoint = FindClosestPoint(target)
		if nearestPoint then
			script.Parent.HumanoidRootPart.CFrame = nearestPoint.CFrame
		end
	end
	wait(1)
1 Like

What is the issue? You need to describe the problem and what you want to happen instead in the post.

The problem is that NPC does not always appear in front of the target and does not attack.

Try adding print()s to each of the places where you directly move the NPC with HumanoidRootPart.CFrame (also, there’s one with .Position instead of .CFrame, change that to .CFrame) to see which one is moving it.

What does FindClosestPoint do? Does it look at the visible squares to find the one nearest to the player? Maybe some squares are invisible?

The squares are all visible, and the position must be equal to the corresponding result.

So, have you tried using prints to check what part of the code is teleporting them so you can figure out what’s causing the issue?

By using the part generator to the desired position and prints, the positions are determined correctly.

Are you using a translator or something? Literally nothing in that sentence makes any sense, this whole post exists because the positions are wrong somehow.

I said that everything is determined correctly, except that the humanoid is buggy trying to activate MoveTo(). The problem, in my opinion, is it.
robloxapp-20231204-1852399.wmv (3,9 МБ)