How to make something happen while pressing a button

So i’m trying to make a script where if u hold down a keyboard button, something happens for example you sprint while ur holding the button down and then u stop sprinting when you let go, i know this is something simple but for some reason i forgot how to do it.

User Input Service might be something you want to use. You could utilize the .InputBegan and the .InputEnded event. An example would be:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Input,GPE)
   if Input.KeyCode == Enum.KeyCode.E and not GPE then
      --Start sprinting
   end
end)

UserInputService.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.E then
      --Stop sprinting
   end
end
2 Likes

Thanks, i just forgot how to do this, so ty