Problem with CFrame thingy

I have a code that is run in a coroutine. It’s supposed to make the NPC look at the target as it moves. But since the loop sets the npc’s position, it isn’t moving and is staying locked in place (although it still faces target).

How can I make it so that it moves and looks at the same time?

Code:

      local function LookAt(targoot)
    		while wait() do
    			local goal = {CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position,targoot.Position)}
    			tweenserv:Create(script.Parent.HumanoidRootPart,TweenInfo.new(2),goal):Play()
    		end
    	end

Do this

CFrame = CFrame.new() -- So it knows it is a CFrame not a variable

Not CFrameVal :grinning_face_with_smiling_eyes:

That doesn’t help much though. I still need the lookat included in the CFrame.

I know then use this:

while wait() do
    CFrame.lookAt(instancePos,targetPos)
end

Ohhh. My bad. I pasted the wrong thingy. Yeh I tried that already and doesnt work.

That’s what I’m doing. And it’s the problem. Because the position constantly stays the same so the NPC can’t move. :pensive:

Ok but do not do tween service cframe.lookat is just as smooth lol

Oh wait! That’s a different thing. I’ll try it. Ty.

Ahhh that was so obvious! xD Tysm. I swear, I’m a good scripter lol! I just dont use Cframes often.

HMMMMMMMMMMMMMMMMM ok :grinning_face_with_smiling_eyes: It is fine

O wait. No. It won’t work since I need the npc to TURN instead of having his orientation instantly set. eee. I have a few ideas tho.

Something that helps with solving bugs like this would be to use print statements to show where where in the script it’s at when you’re doing specific things. You should also try printing out the variables that may be in question to see if they have the values that you are expecting. This has helped me solve countless number of bugs. Or you could just use the Lua debugger.

Already tried that. Printing didn’t help much. I’m experimenting with new things rn. Hopefully I can get it to work.

If you want whole npc do the humanoidrootpart

You said that it was turning instantly instead of slowly turning? Maybe you should try using TweenService. I used it to make a door open and close

Nah. Not whole npc just head. :C

Read script silly! <3

Thats what Im doing. But it’s all good. I got it to work.

1 Like

You should either cframe them with bodygyro or with the root motor6d because setting the CFrame to the same position won’t work and won’t let the npc move either.