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

NPC Suddenly stops attacking after a few times?

Asked by 1 year ago

Hi. My problem is that my NPC stops shooting after a few iterations. I'm using Run Mode on Roblox Studio. The script that makes it function is a descendant of the NPC itself. It is a Server Script. No errors. Attempts: Around 3-6. Outcome: Shoots around 3 - 15 times, then suddenly breaks.

Video (Its really bad quality but you can see that it is making a movement, then stops):

The script (In NPC, NPC in the workspace, not cloned):

muz = Particle Emitter for Bullet Muzzle bullet = literally the bullet effect both animations are parents of the NPC itself

local root = script.Parent.HumanoidRootPart
local hum = script.Parent.Humanoid
local range = 160
local dmg = 15
local char = script.Parent
local idle = char.Idle
local shoot = char.Shoot
local anim1 = hum.Animator:LoadAnimation(idle)
local anim2 = hum.Animator:LoadAnimation(shoot)
local SS = game.ServerStorage
local bullet = SS.Bullet
local tweser = game:GetService('TweenService')
function findTarget()
    for i, target in pairs(workspace:GetChildren()) do
        if target:IsA("Model") and target:FindFirstChild("HumanoidRootPart") and target:FindFirstChild("HumanoidRootPart"):IsA("Part") and target.Name ~= char.Name then
            local HumanoidRootPart = target:FindFirstChild("HumanoidRootPart")
            local Humanoid = target:FindFirstChild('Humanoid')
            local pos = (root.Position - HumanoidRootPart.Position).Magnitude
            if pos < range and HumanoidRootPart.Parent:FindFirstChild("Humanoid").Health > 0 then
                return HumanoidRootPart, Humanoid
            end
        end
    end
end

while wait() do
    local humroot, hum = findTarget()
    if humroot and hum then
        hum:TakeDamage(dmg)
        local clone = bullet:Clone()
        clone.Anchored = true
        clone.Parent = workspace
        clone.Position = char.muz.Position
        clone.CFrame = CFrame.lookAt(clone.Position, humroot.Position)
        local info = TweenInfo.new(
            .1,
            Enum.EasingStyle.Linear,
            Enum.EasingDirection.Out,
            0
        )
        local goal = {}
        goal.Position = humroot.Position
        local tween = tweser:Create(clone, info, goal)
        tween:Play()
        print('Found target')
        root.CFrame = CFrame.lookAt(root.Position, humroot.Position)
        anim2:Play()
        anim1:Stop()
        char.muz.ParticleEmitter:Emit(1)
        anim2.DidLoop:Wait()
        clone:Destroy()
    else
        print('Target no where to be seen')
        anim1:Play()
        anim2:Stop()
        anim1.DidLoop:Wait()
    end
end

0
Maybe use game["Run Service"].Stepped:Connect(function() instead of while wait() do? piggy0129 2 — 1y
0
Maybe. The findTarget() function decides if its a attackable NPC/Player or not, so it honestly depends. I think something is wrong with the findTarget() function. But thank you, ill try it. Finty_james 269 — 1y
0
It completely breaks the concept of firerate in the dummy. It shoots over 100 times per second now. Finty_james 269 — 1y

Answer this question