How to rotate a model towards another roblox studio model?

Hi all, I am making a turret for my game and I want it to rotate towards the player it is attacking. I have a script that detects the closest player, so I just need a script to rotate the model towards the other model. And I also want the model to rotate in Y only

For the normal part I use a script like this:

RunService.Heartbeat:Connect(function()
	local nearestPlayer, distance, direction = findNearestPlayer()
	if nearestPlayer then
		if distance <= 10 then
			script.Parent.Parent.Rotation.CFrame = CFrame.lookAt(script.Parent.Parent.Rotation.Position, nearestPlayer.HumanoidRootPart.Position)
			script.Parent.Parent.Rotation.Orientation = Vector3.new(0, script.Parent.Parent.Rotation.Orientation.Y+180, 0)
		end
	end
end)
1 Like

Sorry for my last message, here is the correct code:

RunService.Heartbeat:Connect(function()
	local nearestPlayer, distance, direction = findNearestPlayer()
	if nearestPlayer then
		if distance <= 10 then
			script.Parent.Parent.CFrame = CFrame.lookAt(script.Parent.Parent.Position, Vector3.new(script.Parent.Parent.Position.X,nearestPlayer.HumanoidRootPart.Position.Y,script.Parent.Parent.Position.Z))
		end
	end
end)

here i say again… i think this MIGHT should work.

1 Like

I found a solution here :grinning:

1 Like

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