Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 6 years ago
Edited 6 years ago

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

0
Had an error there, forgot to add something, but I've edited the code Rare_tendo 3000 — 6y

2 answers

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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))
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. NoirPhoenix 148 — 6y
0
I adjusted ur code and got it working! Thanks so much for your help. NoirPhoenix 148 — 6y
0
ah u did what i did :P NoirPhoenix 148 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

Answer this question