Rotating NPC using bodygyro

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
1 Like

The code looks fine, can you send a picture of it in-game?

sorry for late reply, heres a video of it

EDIT: i gave a part the exact same code as the dummy’s orientation script, but the dummy just ends up leaning

It looks loose. If so, try:

  • Adjusting the BodyGyro’s MaxTorque to Vector3.one * math.huge
  • Increasing the BodyGyro’s P value

For future uses, I’d recommend using an AlignOrientation as it’s not deprecated, works with attachments, and is more versatile overall.

1 Like

bodygyros are deprecated and should be replaced with AlignOrientation or AngularVelocity

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