I think I found the solution, change angles to Angles.
(Difference being the capital A).
robloxapp-20220626-1538037.wmv (2.3 MB)
Alright, it works, but the npcs teleport to the player when they’re close (?)
Maby you could try:
NPC:SetPrimaryPartCFrame(CFrame.new(CF2.Position) * CFrame.Angles(X1, Y2, Z1))
Oh no! Something went horribly wrong?!
Alright listen, that never happened to me before, but this is for another topic. Is the HumanoidRootPart actually connected to the rig and not anchored? Are you using a WeldConstraint?
Correct.
True, but only if the model has a PrimaryPart.
This is the problem I’ve been trying to solve, because I knew it would happen from the start. This is why I believe my solution would have worked better, but doesn’t seem to work?
Alright let me give a revamped example of this one:
and turn it into:
NPC:SetPrimaryPartCFrame(CFrame.lookAt(NPC.PrimaryPart.Position, NPCPos))
Even though it’s way simpler of my original solution, it should push us in the correct direction. I’ve always liked using CFrame.lookAt
, so maybe give it a try?
I do know there is a known issue with this, as the NPCs will look up at you if you go higher, but I have a workaround, so I’ll edit the post and remove this part if you need it.
Very, very odd. Can we see the code once again? I want to see if everything is placed in the correct location.
Nope, just a non anchored rig with rig joints.
Yea, atleast its not too hard of a fix unless there is no way for a primary part.
Yes, If this was not the problem before then I would have just done.
CFrame(npc.pos, player.pos)
Unfotunatly CFrames gets pretty complicated. My method is trying to change the y rotation while keeping the rest the same. I assume your work around had something similar?
Same here! I changed lookAt to this: NPC.HumanoidRootPart.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position, (NPCPos * Vector3.new(1, 0, 1)) + Vector3.new(0, NPC.HumanoidRootPart.Position.Y, 0))
But I have absolutely no idea why it didn’t work. Probably earlier parts of the code, not using CharacterAdded is breaking this.
Maby, I wonder what is says in the output.
Sorry for the big delay, I’m finally on my computer and I’ve made a solution.
Depending on how you plan to use your NPC, if your game is multiplayer or you want a more readable script then you may want to use this script I’ve created.
This script will follow the closest player and do everything you asked.
local NPC = script.Parent
while wait() do
local Target = nil
local TargetDistance = math.huge
for i = 1, #game.Players:GetChildren() do
local Player = game.Players:GetChildren()[i]
local Character = Player.Character
if Character then
local Distance = (Character.PrimaryPart.Position - NPC.PrimaryPart.Position).Magnitude
if Distance < TargetDistance then
Target = Character
TargetDistance = Distance
end
end
end
if Target ~= nil then
if TargetDistance > 6 then
NPC.Humanoid:MoveTo(Target.PrimaryPart.Position)
else
local function PointTowards2D(CF1 : CFrame, CF2 : CFrame) : CFrame
local V1 = CF1.Position
local V2 = CF2.Position + Vector3.new(0, -CF2.Position.Y + CF1.Position.Y, 0)
return CFrame.lookAt(V1, V2)
end
NPC:PivotTo(PointTowards2D(NPC.PrimaryPart.CFrame, Target.PrimaryPart.CFrame))
end
end
end
Otherwise you can still use your script and just replace the part I was doing with:
local function PointTowards2D(CF1 : CFrame, CF2 : CFrame) : CFrame
local V1 = CF1.Position
local V2 = CF2.Position + Vector3.new(0, -CF2.Position.Y + CF1.Position.Y, 0)
return CFrame.lookAt(V1, V2)
end
NPC:PivotTo(PointTowards2D(NPC.PrimaryPart.CFrame, otherNPC.PrimaryPart.CFrame))
The PointTowards2D makes a CFrame point from one vector to the other only on the Y axis.
It works by setting the Y position on the second vector to be the same as the one in the first.
I recommend not using CFrame.new(Vector3, Vector3)
. It has been deprecated and you should now use CFrame.lookAt(Origin, LookAt)
. I recommend editing your post.
Ill make the change, I didn’t know it was deprecated thanks!
I’ve also done some research on :SetPrimaryPartCFrame()
vs :PivotTo()
And it sounds like :PivotTo()
has better performance
And you mentioned earlier how you need a primary part for :SetPrimaryPartCFrame()
You dont need one for :PivotTo()
Hi, sorry for not answering quickly. I don’t know why but the DevForum just didn’t give me the notification for your reply, I just found it randomly by checking here again. I’ll try what you mentioned, thanks in advance!
I tested the first script out, this happened:
The NPCs follow the player correctly and the direction isn’t messed up or anything, but sometimes it’s a bit choppy and when the player stops, they continue to get close to the player and then teleport backwards
I would send a video, but, boo-hoo, DevForum is being trashy as always!
What script are you running it in, a server script or a local script?
If its a local script, is the model also local?
server script, and it’s inside the npc
Are you able to create a base plate with just the npc and the script, send it to me and I can take a closer look at it?