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
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
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.