How do I make it so the enemy looks at the player and not teleports to them?

I have an aggro script which makes the player move and look at the player (which it should do) but instead it just teleports the boss to the player which I don’t want it to do I want it to look at the player.

Clip of the boss teleporting to the player :
https://gyazo.com/c0f1d9603afbfc8abf23b5f599c5c28b

Aggro script :

local myHuman = script.Parent:WaitForChild("Boss")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local myHead = script.Parent:WaitForChild("Skull")
local tweenService = game:GetService("TweenService")
local curTarget
script.Parent.Music:Play()
function findTarget()
	local target
	local dist = 100000
	for i,v in pairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local rootPart = v:FindFirstChild("HumanoidRootPart")
		if human and rootPart and v ~= script.Parent then
			if (myRoot.Position - rootPart.Position).magnitude <= dist and human.Health > 0 then
				dist = (myRoot.Position - rootPart.Position).magnitude
				target = rootPart
			end
		end
	end
	
	return target
end

function checkSight(target)
	local ray = Ray.new(myHead.Position,(target.Position - myHead.Position).Unit * 1000)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
	
	if hit then
		if hit:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then
			if math.abs(hit.Position.Y - myRoot.Position.Y) < 2 then
				return true
			else
				return false
			end
		else
			return false
		end
	else
		return false
	end
end

while true do
	local target = findTarget()
	if target then
		local sight = checkSight(target)
		if sight == true then
			repeat wait()
				myHuman:MoveTo(target.Position)
				myHead.Parent:SetPrimaryPartCFrame(target.CFrame)
			until checkSight(target) == false
		elseif sight == false then
			local path = game:GetService("PathfindingService"):CreatePath()
			path:ComputeAsync(myRoot.Position,target.Position)
			
			if path.Status == Enum.PathStatus.Success then
				for i,v in pairs(path:GetWaypoints()) do
					myHuman:MoveTo(v.Position)
					
					if v.Action == Enum.PathWaypointAction.Jump then
						myHuman.Jump = true
					end
					
					if checkSight(target) == true then
						break
					end
					
					local timeOut = myHuman.MoveToFinished:Wait()
					
					if not timeOut then
						break
					end
				end
			else
				local dist = 1000
			end
		end
	end
	task.wait()
end

There are no errors it is just not doing what I want it to do the line where it teleports I think is
Line 50 :

				myHead.Parent:SetPrimaryPartCFrame(target.CFrame)

help would be greatly appreciated thank you.

Try CFrame.lookAt

myHead.Parent:SetPrimaryPartCFrame(CFrame.lookAt(myHead.Parent, target.CFrame)) -- You may need to use CFrame.Position

I died while trying to figure out lookAt send help

2 Likes

The reason it’s teleporting is because :SetPrimaryPartCFrame() sets the primary parts cframe to whatever cframe is put inside that function

I know you probably know that, but knowledge is power

myHead.Parent:SetPrimaryPartCFrame(cframe.new(myHead.Parent.PrimaryPart.Position, target.Position)

do something like this ^

although its kinda goofy (then again i used the torso, not the head

1 Like