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