I know how to make a GUI button that makes you move at a certain speed. For example 15 but, what I don’t know is how to make a button you press from keyboard which is “L” makes you move at a certain speed that is 15 for example.
Here is an example in the localscript I inserted it in StarterPlayerScripts which is parent of StarterPlayer:
local plr = game.Players.LocalPlayer
game:GetService("UserInputService").InputBegan:Connect(function(input, process)
if input.KeyCode == Enum.KeyCode.L then
if plr.Character:FindFirstChild("Humanoid") then
plr.Character:FindFirstChild("Humanoid").WalkSpeed = 15
end
end
end)
I hope this helps. ![]()
For that you gotta use the UserInputService which is filled with useful methods and events that help interact with the user’s inputs. You can check all of the things this service offer, but what we are interested in is the InputBegan event which fires whenever an input has done its thing, and not just keyboard buttons, and much more than that, here are all of them. This event gives back 2 paramaters, the first being the input that fired that event, and second is not that important, it’s a boolean that can be used to check if the player was chatting while he used that input. And then you just check if the input’s KeyCode was the KeyCode that you were expecting.
local UserInputService = game:GetService("UserInputService") --the service
UserInputService.InputBegan:Connect(function(input) --input the is what the user pressed
if input.KeyCode == Enum.KeyCode.L then
print("pressed that L")
end
end)
If you’re meaning like a sprint button which you hold down for it to set your WalkSpeed then I’d use ContextActionService & compare the UserInputState.
For example:
--// Dependencies
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
--// Variables
local SPRINT_SPEED = 32
local LocalPlayer = Players.LocalPlayer
--// Function
local function sprint(_, state)
local humanoid = LocalPlayer.Character and LocalPlayer.Character:FindFirstChild("Humanoid")
if state == Enum.UserInputState.Begin then
humanoid.WalkSpeed = SPRINT_SPEED
elseif state == Enum.UserInputState.End then
humanoid.WalkSpeed = 16
end
end
--// Binds
ContextActionService:BindAction("Sprint", sprint, Enum.KeyCode.L)