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.
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)
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.
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)
--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.
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.