So there’s a couple minor things you can do a lot better. Let’s start with using ContextActionService for this specific instance, as UserInputService is more for advanced use cases. It’s also easier to implement on other devices.
local Player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")
Now that we have that, let’s make sure to only sprint when the F key is held down, and stop sprinting when released. This will allow you to only use one key and free up the entirely un-needed use of H. Let’s start with making two functions, one that changes the humanoid walkspeed, and the other to be used by input service. I’ll explain why this is important in a second.
local Player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")
local function ChangeWalkspeed(walkspeed)
--first let's make sure the character and humanoid exists.
local character = Player.Character
if character ~= nil then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid ~= nil then
humanoid.WalkSpeed = walkspeed
end
end
end
local function SprintHandler(actionName,userInputState,inputObject)
--this recieves parameters from contextactionservice that we could use.
if userInputState == Enum.UserInputState.Begin then
ChangeWalkspeed(25)
elseif userInputState == Enum.UserInputState.End then
ChangeWalkspeed(16)
end
end
As you can see, we do a lot of checks on Walkspeed, this is so we can make sure the character and everything we need exists before doing anything. This allows us to avoid running into errors when let’s say, the player’s character is suddenly deleted. The reason we wrap this in a function is to avoid repeating ourselves, which in general is a frowned-upon practice and one of the big reasons functions exist.
As for the second function, it recieves parameters from ContextActionService that we can use. For instance, we can check if the F key is being either pressed down or released. We can use this to then determine whether what speed we want to give the character, simply by checking if the key was pressed down or released. In our case, let’s go with 25 when pressed down, and 16(default) when released. You may change this at your leisure.
Now, on to the last and simplest stage, let’s simply bind this action, to “Sprint” under the Keycode of F.
ContextActionService:BindAction("Sprint",SprintHandler,true,Enum.KeyCode.F)
Here’s the entire script, feel free to play around with it. There’s definitely a lot to learn from:
local Player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")
local function ChangeWalkspeed(walkspeed)
--first let's make sure the character and humanoid exists.
local character = Player.Character
if character ~= nil then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid ~= nil then
humanoid.WalkSpeed = walkspeed
end
end
end
local function SprintHandler(actionName,userInputState,inputObject)
--this recieves parameters from contextactionservice that we could use.
if userInputState == Enum.UserInputState.Begin then
ChangeWalkspeed(25)
elseif userInputState == Enum.UserInputState.End then
ChangeWalkspeed(16)
end
end
ContextActionService:BindAction("Sprint",SprintHandler,true,Enum.KeyCode.F)