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

making npc look at me while attacking ?

Asked by 4 years ago

I want to make my npc to rotate at me while attacking, but the problem is while it's attacking the npc rotate at me immediately and sometimes I get stuck in it's head. this happens because the npc CFrame changes to my CFrame when attaking and I want it to rotate at me smoothly.

this is the code :

local npc=script.Parent
local attackDistance=19
while wait() do
    for i,v in pairs(workspace:GetChildren()) do
        if v:IsA("Model") then
            if v:FindFirstChild("Humanoid") then
                if game.Players:GetPlayerFromCharacter(v)~=nil then
                    npc:SetPrimaryPartCFrame(CFrame.new(npc.PrimaryPart.Position ,Vector3.new(v.HumanoidRootPart.Position.X, npc.PrimaryPart.Position.Y, v.HumanoidRootPart.Position.Z) ))
                    if (v.HumanoidRootPart.Position-npc.HumanoidRootPart.Position).magnitude<attackDistance then                        
                        startAttack() --call attack function
                        wait(1.5)
                    end
                end
            end
        end
    end
end

Answer this question