Is there anyway you could make the model look at the nearest player?

I want the model to look at the player, but i cant figure it out.

Any link/dev post/script would be good.

Thanks.

2 Likes

Wdym with look at the nearest player? Do u mean that you want the model to be facing to a certain direction?

1 Like

I want the model to be facing at the player.

2 Likes

Here:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Model = workspace.Model
local function GetNearestPlayer()
	local LowestMagnitude, Player = math.huge, nil
	for i, v in pairs(Players:GetPlayers()) do
		if v.Character == nil or v.Character.PrimaryPart == nil then continue end
		local Minimum = math.min(LowestMagnitude, (v.Character.PrimaryPart.Position - Model.PrimaryPart.Position).Magnitude)
		if Minimum ~= LowestMagnitude then
			LowestMagnitude = Minimum
			Player = v
		end
	end
	return Player
end 

while RunService.Heartbeat:Wait() do
	local NearestPlayer = GetNearestPlayer()
	if NearestPlayer == nil then continue end
	Model:SetPrimaryPartCFrame(CFrame.new(Model.PrimaryPart.Position, NearestPlayer.Character.PrimaryPart.Position))
end 

Maybe try this, I haven’t tried it. There might be a better way but try it.
EDIT: I edited the code and by the way, make sure the Model has a PrimaryPart.
EDIT2: I edited the code once again it had some errors.

4 Likes

Ok thanks, you seem lilke a talented scripter.

2 Likes

Here’s a very simplified method : )

local targetPlayer = game.Players:WaitForChild("bigha_rn")

targetPlayer.CharacterAdded:Wait() --Wait until the character has loaded in

while true do

script.Parent:SetPrimaryPartCFrame(CFrame.lookAt(script.Parent.PrimaryPart.CFrame.Position, targetPlayer.Character.HumanoidRootPart.Position))

wait()

end
3 Likes

The one thing I’d caution is that continuous use of SetPrimaryPartCFrame can cause model “drift” as seen here:

3 Likes

That does not get the nearest character; and will only work for bigha_rn.

2 Likes

Oops! Missed the nearest player part. It was implied that the target would be switched out for whichever player OP needed to reference.

1 Like

I mean, is good but at the same time is not because it will be really expensive for big models (models with many parts). Spamming that custom function in a loop I don’t think will be a good idea, but actually thank you for posting that. :smiley:

2 Likes

This causes some drift and isnt very accurate, im sorry if im being picky lol

2 Likes

What do you mean by some drifts and it isn’t very accurate?

Well, it dosent look at me that good, and it moves sometimes.

How laggy is your game? If it doesn’t look at you that good is because the PrimaryPart’s Orientation is not facing the right way right?

EDIT: Sorry my computer is lagging but look, I took a free model building and tested the code and it works…
Flat Terrain - Roblox Studio - Gyazo

The orientation is set as 0,0,0.

local Players = game:GetService("Players")

local model = workspace.Model

local function GetNearestPlayer()
	local lowestDistance, player = math.huge, nil
	for _, player in ipairs(Players:GetPlayers()) do
		if (player.Character == nil or player.Character.PrimaryPart == nil) then continue end
		local minimum = math.min(LowestMagnitude, (player.Character.PrimaryPart.Position - Model.PrimaryPart.Position).Magnitude)
		if (minimum ~= LowestMagnitude) then
			lowestDistance, = Minimum
			player = v
		end
	end
	return player 
end 

while (GetNearestPlayer() ~= nil and wait(1)) do
               
    local nearestPlayer = GetNearestPlayer()
	Model:SetPrimaryPartCFrame(CFrame.lookAt(Model.PrimaryPart.Position, NearestPlayer.Character.PrimaryPart.Position))
end 

I optimized your code a bit and you should use CFrame.lookAt rather than CFrame.new because it’s deprecated.