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)