How do i make an eye follow the nearest player?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    An interminable rooms E section eyeball that follows the nearest player.

  2. What is the issue? Include screenshots / videos if possible!
    I dont know how to code it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Alot of posts.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

You can use either pathfindingservice or human:MoveTo() and do this in a while loop.

Get the targets(players) by doing this:

local function FindVictim()
for i, char in workspace:GetChildren() do
if char:FindFirstChildOfClass("Humanoid") then
local target = char
return target
end
end
end

not like that, like it looks at the player.
i have tried using cframe.lookat(), and failed.
an easier way to get the players is by just doing game.Players:GetPlayers()

I think doing game.Players:GetPlayers() is worse but each to their own. Also it should automatically look at the player by going towards it anyway.

i dont want it to literally follow the player. i want it to look at them.
atleast have some common sense.

1 Like

Sheesh sorry for trying to help you.

I think he means the same way Doors does. The eyes on the walls look at the nearest player.

1 Like

give me the prompt of your script describe it in 3-5 sentences. then ill maybe help you bruhhh

Yeah, thats what i want
but idk how to make it

Please do not ask people to write entire scripts or design entire systems for you.

But seriously you can get the distance between the players and the eyes by doing

local distance = (workspace.Eye.Position - target.Position).Magnitude
if distance <= 40 then
end
1 Like

You’ll have to make two systems. First, a system for finding the nearest player.

To make the eye look at the character, if you want it to be exactly like doors in that the pupil inside looks at the character, use this equation:

local pointAt = (character:GetPivot().Position - eye.Position).Unit * radiusOfEyeball

eye.Position = eyeball.Position + pointAt

This is assuming eye is the pupil, and eyeball is the eyeball, and they are both spheres.

You’ll also have to clamp it, but to do that you’ll need to predefine the direction the eyeball is pointing in.

local vectorDirection = Vector3.new(0, 0, -1) -- forward
local pointAt = (character:GetPivot().Position - eye.Position).Unit * radiusOfEyeball

if (pointAt - vectorDirection).Magnitude < someMaxValue then
    eye.Position = eyeball.Position + pointAt
end

Not tested.

lookAt is (not) deprecated and was (not) replaced with CFrame.new(pos,lookAt)

local part = script.Parent
local newPart = workspace.NewPart

part.CFrame = CFrame.new(part.Position,newPart.Position)

what this script does is make the 1stPart look at 2ndPart. Which is pretty much what you want.

Edit: lookAt isn’t deprecated and in fact is the replacement for CFrame.new(pos,lookAt) You should in fact be using LookAt Thanks to @Amritss for pointing this out

3 Likes

It works! :smiley:
Heres my code:

game["Run Service"].PreSimulation:Connect(function()
	local MaxDistance = 1000000
	local NearestPlayer = nil
	for i, Player in game.Players:GetPlayers() do
		if Player and Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
			if (Player.Character.HumanoidRootPart.Position.Magnitude - script.Parent.Position.Magnitude) <= MaxDistance then
				NearestPlayer = Player
			end
		end
	end
	if NearestPlayer and NearestPlayer.Character and NearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
		script.Parent.CFrame = CFrame.new(script.Parent.Position,NearestPlayer.Character.HumanoidRootPart.Position)
		script.Parent.Orientation += Vector3.new(180, 0, 180)
	end
end)

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