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

Why wont ":SetPrimaryPartCFrame" not allow math equations to make an NPC face another NPC?

Hello i am trying to get NPCs to fight each other based around attack distances something i have noticed is sometimes the NPCs move slightly to far causing the NPCs to appear facing back to back. I have programmed them to have no walkspeed if they are within attack radius and am trying to get them to correct them selves using ":SetPrimaryPartCFrame()" but i keep getting this error "Unable to cast Vector3 to CoordinateFrame"

Heres the line of code:

child:SetPrimaryPartCFrame(HRP.Position - eHRP.Position)

If anyone knows how i can adjust this to get it to function like it is suppose to I'd really appreciate it.

P.S: I have tried using:

child:SetPrimaryPartCFrame(HRP.Position.X - eHRP.Position.X,HRP.Position.Y - eHRP.Position.Y,HRP.Position.Z - eHRP.Position.Z)

And it still doesn't want to work (same error)

THIS IS NOT A REQUEST

1 comment

0

Had an error there, forgot to add something, but I've edited the code

2 answers

✓ Accepted answer

The reason why this error happens is because you're passing a vector3, when it needed a CFrame, and you're trying to make the npc face the other the wrong way.

CFrames have a constructor that will help you: CFrame.new( Vector3 Pos, Vector3 lookAt )

This will return a CFrame situated as Pos and will face lookAt

Try this:

child:SetPrimaryPartCFrame(CFrame.new(HRP.Position, eHRP.Position))

3 comments

0

Same error lol. Thanks for trying though. I would try the old fashioned way again where you just tp the torso using CFrame but that simply just moves the torso now.

0

I adjusted ur code and got it working! Thanks so much for your help.

0

ah u did what i did :P

The error is pretty clear. SetPrimaryPartCFrame expects a CFrame and you're providing it with "HRP.Position" which is a Vector3 (i.e. contains only X, Y, Z coords).

A CFrame and a Vector3 is not the same thing. CFrames contain a Vector3 AND an orientation whereas a Vector3 is...just a Vector3.

I'm not sure what you need but you can cast it to a CFrame by adding a new CFrame instance or using the current primary part CFrame:

CFrame.new() + (HRP.Position - eHRP.Position)

This creates a CFrame without preserving the character's orientation and moves it to the new world position.

child.PrimaryPart.CFrame + HRP.Position

This creates a new CFrame and translates it using the Vector3 "HRP.Position."

http://wiki.roblox.com/index.php?title=CFrame#CFrame_.2A_Vector3

*I have not tested this code

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