How to make a part that chases after a player with CFrames

I want to make a part that chases the closest player with CFrames (kind of like teleporting towards the closest player). I am not sure how to make it move towards the player like Humanoid:MoveTo() would. Here is my code:

local BossAct = game.ReplicatedStorage:WaitForChild("BossAct")
local BossHRP = script.Parent --part
local FedUpPart = script.Parent.Parent --model 
local MaxDistance = math.huge
local Sounds = game.SoundService
BossAct.Event:Connect(function()
	print("Boss Begins")
	local players = game.Players:GetPlayers()
	local closest
	for i, plr in pairs(players) do
		if plr.Character and plr.Character:FindFirstChild("Humanoid") and plr.Character.Humanoid.Health > 0 then
			local PlayerHumanoidRootPart = plr.Character.HumanoidRootPart
			local Distance = (BossHRP.Position - PlayerHumanoidRootPart.Position).Magnitude
			if not closest then
				closest = PlayerHumanoidRootPart
			end
			if (BossHRP.Position - closest.Position).Magnitude > Distance then
				closest = PlayerHumanoidRootPart
			end
			print(PlayerHumanoidRootPart)
		end
	end
	if closest and (BossHRP.Position - closest.Position).Magnitude <= MaxDistance then 
		while task.wait(0.5) do
			Sounds.scary:Play()
			BossHRP.CFrame = CFrame.new(closest.Position + Vector3.new(0,10,0), closest.Position) --just teleports above closest player
			print(BossHRP.Position)
			print(closest.Position)
		end
	end
end)
1 Like

use humanoid.MoveTo(CFrame.Position)

You could use CFrame.lookAt to get a cframe where the part looks at the player then move the part towards the lookvector of that cframe

this solution works but you should probably just make the boss have a humanoid so you can do humanoid:MoveTo()

2 Likes

Can’t use humanoid:MoveTo() because it’s anchored, thanks for the other suggestion

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.