Cooldown with text Popup (Please wait 5 seconds...)

How to add cooldown in a script with text pop up?

  • Let’s take this simple gui teleport script as an example
    • local part = workspace.TeleportPart
      local plr = game.Players.LocalPlayer

    • script.Parent.MouseButton1Click:Connect(function()
      local humanoidrootpart = plr.Character:FindFirstChild(“HumanoidRootPart”)
      if humanoidrootpart then
      humanoidrootpart.CFrame = CFrame.new(part.CFrame.p) + Vector3.new(0,5,0)
      end
      end)

===========================

  • What do I want it to do?
    – I want it to have a cooldown of 5 seconds.
    – I want it to be connected with textlable saying " Please wait 5 seconds… "

===========================
– I can add variables to get the Textlabel I want and connect it with code.
– I can make it pop up using Tween service.

  • The things I need help with is:
    – I need a code which makes 5 seconds cooldown function
    – I need a code which changes text according to the time remaining.
    – I want the new text pop up to push the old pop up above and not overlap each other.

===========================
I am active here for next few hours, will be waiting patiently and will reply fast :))

9 Likes

add this after Vector3.new:

local x = 5
while x <= 5 do
    [GUI].Text = "Cooldown ("..tostring(x)..")"
    x = x - 1
end

[GUI] is the GUI’s location.

3 Likes

You could do this
LuaScript–
Local Start = true
–Add a function here
If Start == true then
Start = false
–wait 5 seconts pop up
Script here
Wait(5)
Start = true
end
end)

2 Likes

Somthin like that thats called a debounce btw

3 Likes

I just typed that stuff may be misspelled or might not work but look up debounce if it dont and use that

1 Like

Like this?

2 Likes

replace the “Cooldown (”…tostring(x)…“)” with this:
textLabel.Text = "Please wait "..tostring(x).." seconds"
If you want it the label to stay at “Please wait 5 seconds”, replace the code I gave you with this:

textLabel.Text =  "Please wait 5 seconds"
wait(5)
-- anything else you want to do goes here like closing the GUI etc.
2 Likes
    1. I wanted this to show up only after the button is being pressed more than once
    1. After text is 0, it’s doing countdown in negative digit as u can see below
    1. I want text to go invisible after the count down is 0.
      image
1 Like

Use this:

local x = 5
while true do
    wait(1) -- oops forgot to add a wait
    [GUI].Text = "Cooldown ("..tostring(x)..")"
    x = x - 1
end

I don’t see the difference, it’s still going below 0 in negative.

I’m pretty sure there’s an alternative to MouseButton1Click to identify double clicks.

1 Like

Oh I thought you wanted it to countdown in negative digits. Then replace the loop with:

local x = 5
while x <= 5 do
    wait(1) -- oops forgot to add a wait
    [GUI].Text = "Cooldown ("..tostring(x)..")"
    x = x - 1
end

[GUI].Visible = false 

I might have made a mistake in the original code. Sorry!

2 Likes

Put it before the wait 5 and take it out after the wait 5

1 Like
local part = workspace.TeleportPart
local plr = game.Players.LocalPlayer

local textlabel = script.Parent.TextLabel
local x = 5

script.Parent.MouseButton1Click:Connect(function()
local humanoidrootpart = plr.Character:FindFirstChild(“HumanoidRootPart”)
if humanoidrootpart then
humanoidrootpart.CFrame = CFrame.new(part.CFrame.p) + Vector3.new(0,5,0)
local x = 5
while true do
    wait(1) -- oops forgot to add a wait
    textlabel.Text = "Cooldown ("..tostring(x)..")"
    x = x - 1
if x <= 0 then 
textlabel.Visible = false
break
end
end
end
end)

this should break the loop and set the textlabel’s visible to false once it hits 0, and of course add a debounce to the mousebutton1click because they could activate it again if the cooldown is on.

It would be more efficient to use <= like:

Why not use a for loop?

-- db = Debounce (start with: local db = true)
if db == false then return end
db = false
--start 5,end 0,count -1
for i = 5,0,-1 do -- i = current number
     textlabel.Text = "Please Wait " .. i  .. " seconds..." -- "Text" .. number .. "Text"
     task.wait(1) -- speed of the count
end 
-- continues when start = end
db = true
print("Done")
1 Like

Everyone here did this incorrectly lol.

Here’s a precise cooldown:

local timeleft = 5 --seconds
while timeleft > 0 do
timeleft -= task.wait(1)
textLabel.Text = "Please wait "..math.round(timeleft).." seconds..."
end
--the timer is done!

pop-up part:

local newPopup --replace with your new gui
local oldPopup --replace with your old gui
oldPopup:TweenPosition(Udim2.new(1, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 1)
newPopup:TweenPosition(oldPopup.Position, Enum.EasingDirection.Out, Enum.EasingStyle.Back, 1)
task.wait(1)
oldPopup:Destroy()

You can add these to your current code respectively.

Yeah you are kind of right because I tried all of those and it made not alot of sense.
I am going to try this right now!

I did it and this happened, its showing an error:

You didn’t specify them in your code yourself.