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

How to make a cooldown period between attacks?

Asked by 4 years ago
Edited 4 years ago

With this current script, the NPC just spams the attack over and over. I tried to keep it from spamming but I can never get it to work. The spam also keeps the slash sound from playing after the first time.

local larm = script.Parent.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 30
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                local cool = false
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
                if cool == false then
                    if (temp.Position - pos).magnitude <= 4 then
                        cool = true
                        local anim = Instance.new("Animation")
                        anim.Name = "Slash"
                        anim.AnimationId = "http://www.roblox.com/Asset?ID=27432559"
                        anim.Parent = script.Parent.Parent
                        local track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
                        track:Play(0)
                        script.Parent.Handle.SwordSlash:Play()
                    end 
                end
            end
        end
    end
    return torso
end

while true do
    wait(1)
    local target = findNearestTorso(script.Parent.Parent.HumanoidRootPart.Position)
end 

Edit: Removed questionable unused script. Edit 2: Found out that he never stops attacking even after I move way away.

0
this isnt the attacking script The_Pr0fessor 595 — 4y
0
this is just a find nearest player and move to them script The_Pr0fessor 595 — 4y
0
I didn't mean to paste the bottom part. That doesn't actually happen. I'm using the findNearestTorso so that the NPC knows when to attack. KuroInteriKanashii 4 — 4y

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Let's use a debounce and use spawn for our cooldown

I'll create variable called db and set it to true, when the attack happens i will check if it is true, if so setting it to false and then using spawn to put waiting a certain amount of time (cooldown) and setting db to true in another thread.

I am assuming that the code after

if (temp.Position - pos).magnitude <= 4 then

is the attack

--my variable
local db=true

local larm = script.Parent.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 30
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                local cool = false
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
                if cool == false then
        --i add check for db
                    if (temp.Position - pos).magnitude <= 4 and db==true then
            --set db to false
            db=false
                        cool = true
                        local anim = Instance.new("Animation")
                        anim.Name = "Slash"
                        anim.AnimationId = "http://www.roblox.com/Asset?ID=27432559"
                        anim.Parent = script.Parent.Parent
                        local track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
                        track:Play(0)
                        script.Parent.Handle.SwordSlash:Play()
            --new thread using spawn
            spawn(function()
                --cooldown time
                wait(5)
                --set db to true
                db=true
            end)
                    end 
                end
            end
        end
    end
    return torso
end

while true do
    wait(1)
    local target = findNearestTorso(script.Parent.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        wait(0.2)
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end 
end 
0
I ran that version, and it did nothing to change what is happening. The attack is still spammed and no errors are printed. KuroInteriKanashii 4 — 4y
Ad

Answer this question