Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a cooldown period between attacks?

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.

3 comments

0

this isnt the attacking script

0

this is just a find nearest player and move to them script

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.

1 answer

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 

1 comment

0

I ran that version, and it did nothing to change what is happening. The attack is still spammed and no errors are printed.

This discussion is preserved as it was. New answers, comments, and votes are closed.