How to use CFrames to point a gun at the player

I’m trying to make a NPC that when you get near him he starts shooting at you. I have completed the AI and animations but the arms don’t point up if you go above him, which is a problem cause then the bullet doesn’t shoot at the player. The NPC is R15.

Maybe when the player is near the NPC, you can lift up the NPC’s right arm 90 degrees, then try to find the from surface of the right arm, then use a while true loop to make the right arm’s front surface to face at the player.

while true loop
    local currentPosition = NPC.RightArm.Position
    NPC.RightArm.CFrame = CFrame.new(currentPosition, player.HumanoidRootPart.Position)
end

Ive tried using this but when the code gets executed:
Rotation is around arm????

The rotation is around the arm. I need the arm to face the target

Oh, forgotten that the model has motors, change it the RightArm to the HumanoidRootPart.

Now the Cop just spins around the player. Do I have to change both of the RightArms?

Why did it do that? Have you made sure you typed:

while true do
    local currentPosition = NPC.HumanoidRootPart.Position
    NPC.HumanoidRootPart.CFrame = CFrame.new(currentPosition, player.HumanoidRootPart)
end

That makes the whole character face at the player. I just want his arms to face at them like hes holding a gun.

If you don’t this, the NPC would just stand still and rotate the right arm. Seems normal right? What if the player is behind the NPC? The arm will turn back, making it look like the NPC breaks his joint and not logical.

I already fixed that when I first created the AI

That cop would scare someone to death. Looks like he is doing some form of a sacred ritual around you XD

2 Likes