Scripting Helpers is winding down operations and is now read-only. More info→
Log in to vote

How to change the key?

Asked by 8 years ago

I created this script following some instructions but I am not sure how I would change it so that if the F key is hit you sprint but if the F key is hit again you stop sprinting. I also would like an explanation as to how this script exactly works as I see this string stuff and and I am not sure what it even does.

--Put in Game.StarterPlayer.StarterPlayerScripts

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
local mouse = player:GetMouse()

LastTap = math.floor(tick())

    key = key:lower()
    if key == string.char(48) then
    character.Humanoid.WalkSpeed = 30


    key = key:lower()
    if key == string.char(48) then
        character.Humanoid.WalkSpeed = 16

1 answer

Log in to vote
Answered by
EgoMoose 802 Moderation Voter
8 years ago

My first suggestion would be to use the UserInputService since KeyDown and KeyUp are deprecated methods of getting user input.

With that in mind there are two events we need in the UserInputService:

The first event, InputBegan, fires when the user presses a key down or their mouse up. The second event, InputEnded, fires when the user stops pressing a key down or up.

These events also give us something called input objects:

One of their properties is the KeyCode property:

It returns an enum of the key pressed (assuming it exists otherwise nil). The enum list can be found here: remeber though, intellisense is a life saver sometimes.

That being said, we have a enough information now to solve your problem.

local player = game:GetService("Players").LocalPlayer;
local character; repeat wait(); character = player.Character; until character;
local humanoid = character:WaitForChild("Humanoid");

local inputService = game:GetService("UserInputService");

inputService.InputBegan:connect(function(input, process)
    if process then return; end; -- don't continue if player is in menu or something
    if input.KeyCode == Enum.KeyCode.LeftShift then -- player pressed the leftshift key
        humanoid.WalkSpeed = 30;

inputService.InputEnded:connect(function(input, process)
    -- same process as above but when the player stops pressing the key
    if input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = 16;
Thanks! What would the code be though if I wanted to use the F key? Conmmander 479 — 8y

Answer this question