How to make a click "Z" to run script

Hello people, how are you doing? How do I make a script where if a player clicks “Z” on their keyboard, they will start running (or increased walkspeed). And when they click “Z” again, they will stop running (or their walkspeed will return to normal)
So instead of holding “Z”, I just want the player to simply click “Z” on their keyboard so they can run.

Any kind of help would be appreciated.

Using the UserInputService, this can be done pretty easily.

local uis = game:GetService("UserInputService")
local keybind = Enum.KeyCode.Z

local isRunning = false

function InputBegan(input)
    if input.KeyCode == keybind and not isRunning then
       -- Your Run Code
       isRunning = true 
    end
end

function InputEnded(input)
    if input.KeyCode == keybind and isRunning then
       -- Your Run Code
       isRunning = false 
    end
end

uis.InputBegan:Connect(InputBegan)
uis.InputEnded:Connect(InputEnded)
2 Likes

This is not really working. Do I place it on StarterCharacterScripts?

This is a localscript, so I would place it in StarterPlayerScripts or StarterGUI

It works now but I still have to hold “Z” to actually run instead of just clicking “Z”

Unless I am doing something wrong, this code does not let you hold “Z” and only lets you click “Z”

Adding on to this, you can hold “Z” but it still lets you click “Z”

I checked the player’s humanoid everytime I clicked Z, it did increase the wallkspeed, but when I let go of the “Z” it just returns to the normal walkspeed.

Maybe this works?

Instead of using the InputEnded function, inside the InputBegan function, write the following:

if input.KeyCode == keybind then
    if isRunning then
       -- stop running
       isRunning = false
    else
       -- start running
       isRunning = true
    end
end

Delete the InputEnded function and replace code above in the InputBegan function

local uis = game:GetService("UserInputService")
local keybind = Enum.KeyCode.Z

local isRunning = false

function InputBegan(gameProcessed, input)
    if not(gameProcessed) then return end
    if input.KeyCode == keybind and not isRunning then
       -- Your Run Code
       isRunning = true 
    end
end

function InputEnded(gameProcessed, input)
    if not(gameProcessed) then return end
    if input.KeyCode == keybind and isRunning then
       -- Your Run Code
       isRunning = false 
    end
end

uis.InputBegan:Connect(InputBegan)
uis.InputEnded:Connect(InputEnded)
1 Like

i suggest you watch this helpful video to get an idea of how inputs work

humanoid.WalkSpeed doc

https://developer.roblox.com/en-us/api-reference/property/Humanoid/WalkSpeed

once you get the basics of user input service down you might want to change to using context action service

Here’s the code I think you’re looking for.

--This is in a LocalScript under StarterPlayer > StarterPlayerScripts
local contextActionService = game:GetService("ContextActionService")
local players = game:GetService("Players")

local isRunning = false

local player = players.LocalPlayer
repeat task.wait() until player.Character
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local defaultWalkSpeed = 16
local sprintWalkSpeed = 30

function run(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		if isRunning then
			isRunning = false
			--Your run code below
			humanoid.WalkSpeed = defaultWalkSpeed --Just an example
		else 
			isRunning = true
			--Your default walk code below
			humanoid.WalkSpeed = sprintWalkSpeed
		end
	end
end

contextActionService:BindAction("run", run, false, Enum.KeyCode.Z)

While I have created a copy and paste code for you, please keep in mind that it’s best that you learn the concepts and techniques that power my code.

A few resources I suggest you check out:

The following is the service that I used in my code. I personally prefer this over UserInputService, but they both work fine. ContextActionService simply allows for more contextualized input handling.
https://developer.roblox.com/en-us/api-reference/class/ContextActionService

This is the service that @CSSTransitioned used:
https://developer.roblox.com/en-us/api-reference/class/UserInputService

Here is the WalkSpeed property of Humanoid:
https://developer.roblox.com/en-us/api-reference/property/Humanoid/WalkSpeed

The video that @LittleLegender posted is also a great resource.

1 Like

This would ignore all inputs which weren’t processed by the core user interface of the game itself.

local uis = game:GetService("UserInputService")
local isRunning = false
local keyBind = Enum.KeyCode.Z
local player = game.Player.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

uis.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	if input.KeyCode == keyBind then
		isRunning = not isRunning
		if isRunning then
			humanoid.WalkSpeed = 24
		elseif not isRunning then
			humanoid.WalkSpeed = 16
		end
	end
end)

Anyway here’s a run script which works on a keypress (press key once to toggle) as opposed to hold key down to toggle.

InputBegan fires when a key is pressed, InputEnded fires when a keypress ends, as such your code would require the player to hold the run key down as when the key is released the speed is set back to normal.

Thank you for the help everyone. After watching the video and looking into some scripts I’ve managed to make a sprinting script that works as intended. Again, thanks for the help.

When solving scripts, please mark the solution as solved.

1 Like