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

NPC jittering when i'm crawling/rapidly pressing W?

Asked by 3 years ago

My NPC always keeps jittering and stuttering when i'm either rapidly pressing W or crawling.

I tried finding the reason but I can't seem to find it

Any help?

local monster = script.Parent
local humanoid = monster.Humanoid
monster.PrimaryPart:SetNetworkOwner(nil)
local PathfindingService = game:GetService("PathfindingService")

local function canSeeTarget(target)
    local origin = monster.HumanoidRootPart.Position
    local direction = (target.HumanoidRootPart.Position - monster.HumanoidRootPart.Position).unit * 40
    local ray = Ray.new(origin, direction)

    local hit, pos = workspace:FindPartOnRay(ray, monster)

    if hit then
        if hit:IsDescendantOf(target) then
            return true
        end
    else
        return false
    end
end

local function findTarget()
    local players = game.Players:GetPlayers()
    local maxDistance = 40
    local nearestTarget

    for index, player in pairs(players) do
        if player.Character then
            local target = player.Character
            local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

            if distance < maxDistance and canSeeTarget(target) then
                nearestTarget = target
                maxDistance = distance
            end
        end
    end

    return nearestTarget
end
local function getPath(destination)
    local pathParams = {
        ["AgentHeight"] = 4,
        ["AgentRadius"] = 1,
        ["AgentCanJump"] = false
    }
    local path = PathfindingService:CreatePath()

    path:ComputeAsync(monster.HumanoidRootPart.Position, destination)

    return path
end

local function attack(target)
    local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

    if distance > 3 then
        humanoid:MoveTo(target.HumanoidRootPart.Position)
    else
        local attackAnim = humanoid:LoadAnimation(script.Attack)
        attackAnim:Play()
        target.Humanoid.Health = 0
    end
end


local function walkTo(destination)
    local path = getPath(destination)
    if path.Status == Enum.PathStatus.Success then
        for index, waypoint in pairs(path:GetWaypoints()) do
            local target = findTarget()
            if target and target.Humanoid.Health > 0 then
                attack(target)
                break
            else
                humanoid:MoveTo(waypoint.Position)
                humanoid.MoveToFinished:Wait()
            end
        end
    else
        humanoid:MoveTo(destination.Position - (monster.HumanoidRootPart.CFrame.LookVector * 10))
    end

end

local function patrol()
    local waypoints = workspace.waypoints:GetChildren()
    local randomNum = math.random(1, #waypoints)
    walkTo(waypoints[randomNum].Position)
end

while wait(0.5) do
    local target = findTarget()
    if target then
    end
    patrol()
end

0
This is probably due to it having to recalculate where you are whenever you press w. I'm not to sure if there is a fix for this. Trongaming3211 7 — 3y

Answer this question