How I could use wait() without making delay on next lines?

Ok lest say I got a local function. And on the end of that local function I do something for exemple,

local Thing = false

local function Test()
   Thing = true 
   wait(2)
   Thing = false
end)

And If I use wait(2) and If I had something using that local functions this will delay all I need because is waiting for 2 secounds.

I am making a gun(Freezer Ray) and when I shoot and I hit a player it makes a IceCube effects, it makes the player not able to move. but after 2 secounds I make the player able to move again.

The issue is if I do wait(2) the moving bullet will come after wait(2).

Any idea to use any other way for like “skip wait(2) but make it happen”

1 Like

Use coroutine.wrap() or task.spawn() to do this.

3 Likes

How do I apply that in local function?

1 Like

If you are using coroutine.wrap():

coroutine.wrap(function()
   -- Code
end)()

If you are using task.spawn():

task.spawn(function()
   -- Code
end)
1 Like

So in your case:

local Thing = false

local function Test()
    coroutine.wrap(function()
        Thing = true 
        wait(2)
        Thing = false
    end)()

    -- Other Code
end)

or

local Thing = false

local function Test()
    task.spawn(function()
        Thing = true 
        wait(2)
        Thing = false
    end)

    -- Other Code
end)
3 Likes

Oh god thank you. I was thinking about “Is possible to use wait on debris”

1 Like

Btw I recommend using task.wait() instead of wait() since I think that is depricated or is getting debricated.

Especially in task.wrap() it just makes more sense

Yes I am using task.wait() :smiley:

2 Likes