Wait() pauses the script. How do I make my loop run even if there is a wait() function

Hi. So I am making an AI that shoots people. Everything is working but, when I use:
Animation.Stopped:Wait()

It pauses the script until the animation is stopped. So my loop wont continue running until the animation is finished. I expected this but I don’t know how to fix it, what alternatives are there?

If you don’t want wait() to yield the entire thread, then you should use task.spawn(function(). It lets you run a separate thread without stopping the entire code.

1 Like

Ohh I didnt realise task.spawn did that. Thank you. Would it be something like this?

local function AimGun(target)
    Animation:Play()
    Animation.Stopped:Wait()
    print("Stopped")
end

while true do
   print("Working")

   if db == false then
      task.spawn(AimGun(target))
   end
end
1 Like

You’d want:

task.spawn(AimGun(target))

to be:

task.spawn(AimGun, target)

as it takes a function followed by parameters as the arguments. You could also do:

task.spawn(function()
    AimGun(target)
end)
1 Like

ahh thank you very much. it works now

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