Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why am I unable to move eyes with head?

I am making a game and Im using cframe.lookat to make the npc look at the character, the head has small, red, neon balls as children of the head but when I move the head using the script below it doesn't move the eyes with it.


RunService = game:GetService("RunService")
game.Players.PlayerAdded:Wait()

plr = game.Players:GetChildren()

for _, Player in plr do
	
	plr = Player
	break
	
end

character = plr.Character or game.Players[plr.Name].CharacterAdded:Wait()

game.Workspace:WaitForChild("NPC")

print("HERE")

RunService.Heartbeat:Connect(function()
	local npcPosition = game.Workspace.NPC.Head.Position
	local targetPosition = plr.Character.Head.Position


	task.wait()

	game.Workspace.NPC.Head.CFrame = CFrame.lookAt(game.Workspace.NPC.Head.Position, targetPosition)
end)

1 answer

✓ Accepted answer

You can use WeldConstraints to glue the eyes on the head.

-- This should be a Normal Script inside the NPC
local NPC = script.Parent
local Head = NPC.Head
local Eyes = Head.Eyes -- change this if it has a different name

local weld = Instance.new("WeldConstraint", Head)
weld.Part0 = Head
weld.Part1 = Eyes

I edited the script to make the script look organized. I also made it that if you make it a LocalScript the eyes will only move in the player's screen if they move just like Seek's eyes in Doors.

Also you don't need to use RunService.Heartbeat since task.wait() functions the same way.

-- This should be a LocalScript inside StarterPlayer.StarterCharacterScripts
local Character = script.Parent
local Head = Character:WaitForChild("Head")

local NPC = workspace:WaitForChild("NPC")
local NPCHead = NPC:WaitForChild("Head")
local NPCHeadCFrame = NPCHead.CFrame

while true do
	NPCHead:PivotTo(CFrame.lookAt(NPCHeadCFrame.Position, Head.CFrame, NPCHeadCFrame.UpVector))
	
	task.wait()
end

2 comments

0

Yes thank you but now i'm trying to use collection service to make a lot of them look at the character at once but i cant figure out how.

0

You mean multiple NPCs looking at the same person?

This discussion is preserved as it was. New answers, comments, and votes are closed.