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

Help with simple lookVector script for the NPC facing a model?

So this script is inside of a NPC. What I am wanting is for the NPC's torso to face Player1. Error: lookVector cannot be assigned to.

local Figure = script.Parent
local Torso = Figure.Torso

game.Workspace:WaitForChild('Player1')

while wait() do
	Torso.CFrame.lookVector = game.Workspace.Player1.Torso
end

1 answer

✓ Accepted answer

You can't set read-only Values like lookVector.

Instead, you have to create a CFrame to put the new Torso at. There are a number of ways to do this, but the best is to calculate the heading of the line going from the Torso towards Figure.Torso:

local Figure = script.Parent
local Torso = Figure.Torso

while wait() do
    local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = CFrame.new(Torso.Position, workspace.Player1.Torso.Position):components()
    local heading = math.atan2(R02, R22)
    Torso.CFrame = CFrame.new(Torso.CFrame.p, Torso.CFrame.p + CFrame.Angles(0, heading, 0).lookVector)
end

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