Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

4 comments

1

J O J O

0

Yare Yare Daze

0

What would cool down then?

0

I would also recommend you to use "UserInputService" and "ContextActionService" for detecting if a player pressed a key

1 answer

✓ Accepted answer

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!

6 comments

0

How do I use it? I'm not really understanding the script :P

0

Alright, i will edit my answer in a minute...

0

Done... you can also check the link for more info about coroutines.

0

Thx

0

No problem :)

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.