Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a cooldown script without using wait()?

Asked by 5 years ago
Edited 5 years ago

Hello, I'm making a fighting game (JoJo Game) and I'm wondering how do I make a cooldown without using wait(). Maybe your wondering: "Why does he need a cooldown without using wait()", It's because I'm making an attack script with a couple of attacks and if I use wait() it just stops the whole script and it just ruins the immersion.

ZZZ = Mouse.KeyDown:connect(function(key)
            if key == "t" then
                Blast()
            end
            if key == "e" then 
                OHOra()
            end
            if key == "r" then
                StrongOra()
            end
            if key == "f" then
                TimeStop()
            end
            if key == "z" then
                Jump()
            end
        end)
        ZZZ1 = Mouse.KeyUp:connect(function(key)
            if key == "e" then
                Stop()
            end
        end)
        wait(0.5)
        D = false

This is a part of the script. So if I put a wait() in the E attack and I stop the attack I need to wait until I can do the R attack.

Help would be appreciated

1
J O J O Refined_Arcanite 21 — 5y
0
Yare Yare Daze MajinBluee 80 — 5y
0
What would cool down then? Creacoz 210 — 5y
0
I would also recommend you to use "UserInputService" and "ContextActionService" for detecting if a player pressed a key Refined_Arcanite 21 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Hello MajinBluee!

Since you don't want "wait()" to affect your whole script, you can use coroutines!

So basically, each time you use a coroutine you technically make a script inside a script!

It may sound confusing but a coroutine allows you to make your own cooldown system without any interruptions!

Here is an example.

local UIS = game:GetService("UserInputService")
local DELAY = false
local TIMES_PRESSED = 0

local function STUFF()
      print(game:GetService("Players").LocalPlayer.Name .. " pressed the E key based on delay " .. TIMES_PRESSED .. " times!")
      wait()
end

UIS.InputBegan:Connect(function(Input, PROC)
      if Input.KeyCode == Enum.KeyCode.E and not PROC then
      if DELAY == false then
      DELAY = true
      TIMES_PRESSED = TIMES_PRESSED + 1
      STUFF()

      coroutine.resume(coroutine.create(function() -- Use this method to create coroutines and 
use them quickly!

      wait(2)
      DELAY = false
      end))

      for I = 1, 2 do -- Just to prove that it works, you can also remove the loop and you will notice the delay!
      print(I)
      wait(1)
      end
      end
end
end)

So, to create a coroutine, you simply have to use the "coroutine.create()" function inside a variable, and in order to use it, you have to use the "coroutine.resume()" function, you simply have to use the created coroutine as the argument for "coroutine.resume()", and you are all done!

Another example.

local YourCoroutine = coroutine.create(function()
print("Example")
end)

for I = 1, 5 do
coroutine.resume(YourCoroutine)
wait(5)
end

You can also click HERE to get more info about coroutines!

0
How do I use it? I'm not really understanding the script :P MajinBluee 80 — 5y
0
Alright, i will edit my answer in a minute... Refined_Arcanite 21 — 5y
0
Done... you can also check the link for more info about coroutines. Refined_Arcanite 21 — 5y
0
Thx MajinBluee 80 — 5y
View all comments (2 more)
0
No problem :) Refined_Arcanite 21 — 5y
0
I've got a little problem, When I do a couple of the same attack I just can't do the same attack anymore. Can you help me with that? MajinBluee 80 — 5y
Ad

Answer this question