How to set a timer when pressing a keybind?

Okay, I know that there’s a similar topic posted here- but I need a more in-depth explanation how to do this.

In a short summary, my game is a rage game. There’s also a timer in-between jumps. However, from play-testing it and from my peers also play-testing it- they all complained about one thing: not being able to know when to jump. There’s a short delay when the player can jump again, and this game is a big obstacle course.

My question is, how do I make a timer when someone presses the spacebar button? Let’s say, the timer is on for 3 seconds and then it disappears. How do I do that? An example would be very lovely, or a template at that matter.

Have you already figured out how to disable jumping for 3 seconds? That may complicate things in my script which I’m making for you.

Okay, so assuming that you already created a script which disables jumping, and already created the timer script itself but don’t know how to show it - here’s a simple and probably not very efficient way to show the timer. This took me about 30 minutes to plan and write because of some complications.

--Put in StarterPlayerScripts
local UserInputService = game:GetService("UserInputService")

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local JumpCooldown = false

function StartTimer(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.Space then
        if JumpCooldown == false then
            local Timer = PlayerGui.Timer
            JumpCooldown = true
            Timer.Visible = true
            wait(3)
            JumpCooldown = false
            Timer.Visible = false
        end
    end
end)

UserInputService.InputBegan:Connect(StartTimer)

This script wasn’t tested, so if there are any errors you may refer to the API reference here or message me.

Sorry for not getting to you on time, this is flawless! Thank you!

local Timer = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local function OnHumanoidJumped(State)
	Humanoid.JumpPower = 0
	Humanoid.JumpHeight = 0
	if State then
		Timer.Visible = true
		task.spawn(function()
			for i = 2, 0, -1 do
				Timer.Text = tostring(i)
				task.wait(1)
			end
		end)
	elseif not State then
		task.delay(3, function()
			if Timer.Visible then
				Timer.Visible = false
			end
		end)
	end
	task.wait(3.1)
	Humanoid.JumpPower = 50
	Humanoid.JumpHeight = 7.2
end

Humanoid.Jumping:Connect(OnHumanoidJumped)

Here’s everything, including the jump disabling and timer showing. Local script which goes inside a TextLabel instance.

You should define functions locally (that way they are integrated into the local environment which means that they can be referenced and accessed quicker than if they were defined globally). Don’t forget to make use of the “processed” parameter of the “.InputBegan” event so you can ignore input which was directly tied to Roblox’s core gui. I’d also recommend listening to the humanoid’s state type for this as opposed to keyboard input but that’s just a personal preference.