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.

01ZZZ = Mouse.KeyDown:connect(function(key)
02            if key == "t" then
03                Blast()
04            end
05            if key == "e" then
06                OHOra()
07            end
08            if key == "r" then
09                StrongOra()
10            end
11            if key == "f" then
12                TimeStop()
13            end
14            if key == "z" then
15                Jump()
View all 24 lines...

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.

01local UIS = game:GetService("UserInputService")
02local DELAY = false
03local TIMES_PRESSED = 0
04 
05local function STUFF()
06      print(game:GetService("Players").LocalPlayer.Name .. " pressed the E key based on delay " .. TIMES_PRESSED .. " times!")
07      wait()
08end
09 
10UIS.InputBegan:Connect(function(Input, PROC)
11      if Input.KeyCode == Enum.KeyCode.E and not PROC then
12      if DELAY == false then
13      DELAY = true
14      TIMES_PRESSED = TIMES_PRESSED + 1
15      STUFF()
View all 30 lines...

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.

1local YourCoroutine = coroutine.create(function()
2print("Example")
3end)
4 
5for I = 1, 5 do
6coroutine.resume(YourCoroutine)
7wait(5)
8end

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