so i’ve been trying to make an NPC face towards a player while moving through bodygyros, however, the npc just ends up leaning away from the player
(currently im just trying to make the NPC face towards the nearest player)
reading the devforum just returned to me what im currently doing and im really confused
script: (parented to the npc’s model)
local bodygyro = Instance.new("BodyGyro")
bodygyro.D = 500
bodygyro.P = 3000
local npc = script.Parent
local hrp = npc.HumanoidRootPart
bodygyro.Parent = hrp
function GetNearestPlayer()
local players = game.Players:GetPlayers()
local NearestDist = math.huge
local NearestPlr = nil
for _,player in pairs(players) do
if player and player.Character and player.Character:FindFirstChildOfClass("Humanoid") and player.Character:FindFirstChildOfClass("Humanoid").Health > 0 then
local distance = (player.Character.HumanoidRootPart.Position - hrp.Position).Magnitude
if distance < NearestDist then
NearestDist = distance
NearestPlr = player
end
end
end
return NearestPlr,NearestDist
end
while wait(0.1) do
local player,distance = GetNearestPlayer()
if player then
bodygyro.CFrame = CFrame.new(hrp.Position,player.Character.PrimaryPart.Position)
end
end