How can I add a wait without slowing the script?

How can I add a wait without slowing the script down. I want there to be a few seconds between each attack from the npc. any suggestions? heres the script
EDIT: So far figured out courutine , task.spawn and tick doesnt work. They all seem to be delaying it
maybe im using them wrong but idk

local NPCHumanoid = script.Parent.Humanoid
local NPCBOD = script.Parent
local Folder = game.Workspace.Paths-- put your folder name here
local loop = true -- rename it to false if you don't want it to loop
local attackdb = false

NPCHumanoid.Died:Connect(function()
	loop = false
	script.Scream:Play()
	wait(.01)
	script.Parent:Destroy()
end)

local attackanim = NPCHumanoid.Animator:LoadAnimation(script.attack)
local function attackdbfunction  ()
	wait(3)
	attackdb = false
end


if loop then
	repeat
		wait(.001)
		local function GetPlayersWithinDistance(from : Vector3, distance : number)
			local t = {}
			for _, v in pairs(game.Players:GetPlayers()) do
				if v.Character and v:DistanceFromCharacter(from) < distance then
					table.insert(t, v)
				end
				wait(.015)
				if v:DistanceFromCharacter(from) <=60 then
					NPCHumanoid:MoveTo(v.Character:WaitForChild("HumanoidRootPart").Position)
					if v:DistanceFromCharacter(from) <= 5 then
						if attackdb == false then
							attackdb = true
							attackanim:Play()
							local function attackdbfunction  ()
								wait(3)
								attackdb = false
							end
						else
							
						end
					end	
				else
					
				end
				
			end
			return t 
		end
		
		print(GetPlayersWithinDistance(NPCBOD.HumanoidRootPart.Position, 60)) -- prints all players within 10 studs of the part
		wait(3)
		attackdb = false
	until loop == false
end


Heres what I tried for tick btw

if v:DistanceFromCharacter(from) <= 5 then
						if attackdb == false then
							attackdb = true
							attackanim:Play()
							---coroutine.resume(attackdbtime)
							
								local timer = tick() + 5
								if timer() >= tick() then
									attackdb = false
								end 
								
							

							
						else
							
						end
					end	
2 Likes

I believe you can use task.delay

2 Likes

it seems to be doing the same thing as wait()

if attackdb == false then
							attackdb = true
							attackanim:Play()
							task.delay(5, function()
								attackdb = false
							end)

else

You can try using corutine wraps to have a function or something run without affecting other script, task.wait() might work too

Edit: You can use a debounce without needing to use wait or anything similar, you could also try and use os.clock

2 Likes

Hm, you can use task.spawn instead.

if attackdb == false then
							attackdb = true
							attackanim:Play()
							task.spawn(function()
								wait(5)
								attackdb = false
							end)

else
--...
1 Like

Just tried courutine and still delayed it

local attackdbtime = coroutine.create(function()
	wait(5)
	attackdb = false
end)
if attackdb == false then
							attackdb = true
							attackanim:Play()
							coroutine.resume(attackdbtime)
							
							end)

							
						else

this also just delayed it

if v:DistanceFromCharacter(from) <= 5 then
						if attackdb == false then
							attackdb = true
							attackanim:Play()
							---coroutine.resume(attackdbtime)
							task.spawn(function()
								wait(5)
								attackdb = false
							end)

							
						else

I think you may need to describe better what is being delayed and what you want to have happen.

task.delay will not yield the script, for example, these two(first/second) print statements would happen almost simultaneously:

print("first print")
task.delay(5, function()
		print("5 seconds later....")
     end)
print("second print")

you’d get almost instantly:
first print
second print
(then 5 seconds later you’ll see)
5 seconds later…

2 Likes

alright so I found the delay issue but now task.delay straight up wont work now. idk why but heres a vid.
the 5 seconds isnt being waited on before printing

NPCHumanoid:MoveTo(v.Character:WaitForChild("HumanoidRootPart").Position)
					if v:DistanceFromCharacter(from) <= 5 then
						if attackdb == false then
							attackdb = true
							attackanim:Play()
							
							print("cheese")
							task.delay(5, function()
								
								print("I waited for this cheese")
								attackdb = false
							end)
							print("Crackers")
						else
							
						end
1 Like

might not understand what you mean but
this seperates cooldown from your other code (might not be true but i’ve been using it for a bit)

 if db == true then

spawn(function() --cooldown
db = false
task.wait(cd)
db = true
end)

--do stuff
1 Like

You can see that the ‘i waited for this cheese’ doesn’t show up in the output until 5 seconds in. So the delay is working. But it seems you are calling the function over and over because the ‘i waited’ starts spamming the output over and over after the 5 seconds.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.