What is the Best way to Program a Sprint Script?

I understand this is an inquiry involving preference, but I’d like to address it as there are many routes I could take to create one; collecting your opinions would really help me out!

  1. Variables - A variable containing the actual stamina value is compulsory for convenience. However, regarding camera manipulation, would you create variables containing minimum and maximum FOV values for the script to reference? Or would you simply alter the FOV in proportion with the client’s speed? Would you create a variable containing the keycode to initiate and terminate the sprint for convenience? Would you store a boolean determining whether the client is sprinting or not? Would you store a boolean determining whether the client is able to restart sprinting again (debounce)?

  2. Initiation - I’m having trouble imagining using anything other than UserInputService for this. Most people reading this would generally have two functions with one binded to InputBegan and the other being binded to InputEnded, right? Those two functions could reference the same variables such as debounce and keycode ones.

  3. Conditionals - For initiating the sprint, you’d obviously have to verify that the player isn’t currently running. To get a little fancy, perhaps you could verify that the player is already walking (you can’t sprint while idle) and the stamina value is over a certain amount. As for ending the sprint, you’d obviously have to verify that the client is already running. You’d want to end the sprinting automatically when:

  • The player releases the key
  • The stamina is alarmingly low or zero
  • The player stops moving
  1. Main - How would you alter the FOV? Would you lerp from the current FOV to your desired FOV? What even is the value of your desired FOV? Is it hard-coded? Is it always 1.3x the default FOV? Would you use TweenService, or even an old-fashioned for loop? Would changing the speed occur only after the FOV is finished being increased, or while it is? Would the speed of the client’s character deteriorate with stamina? If so, how often would it update? Speaking of frequency, how quickly would stamina deteriorate, and how quickly would it regenerate? When the player finishes sprinting, what would be a good method for delaying then beginning the regeneration process?

There are probably more factors I would appreciate accumulating opinions of, but I’m absolutely overjoyed with your patience in reading this vague and lengthy post. I will try to implement all your suggestions and hopefully create a decent sprint mechanic.

3 Likes

To handle your sprint ‘conditionals’ I would recommend you implement a state-machine to gracefully handle the logic.

2 Likes

I didn’t bother to read the entirety of your thread because it seems like a lot of questions for something simple, the way I handle sprinting is fairly easy, I’ll answer in co-ordinance with your post:

  1. Variables: For the variables I normally do something like this
--(This variable gets set as soon as the player spawns in with th initial walkspeed)
local MinSpeed = Humanoid.WalkSpeed 
local MaxSpeed = INT
--//Same concept here
local MinFOV = Camera.FieldOfView
local MaxFOV = INT

--//Ratio of the Camera's FOV to the Speed so when the speed increases we multiply the FOV by this value
local SpeedFOV = MaxFOV/MaxSpeed

--///Conditional Values
local IsSprinting = false
local IsPressing = false
  1. Initiation: Normally this wouldn’t really matter at all to be honest, you could use ContextActionService or InputService, I’d stick with InputService for simplicity
UIS.InputBegan:Connect(function(object, gps)
     if not gps and object.KeyCode == Enum.KeyCode.LeftShift then
           isPressing = true
     end
end)

UIS.InputEnded:Connect(function(object, gps)
     if not gps and object.KeyCode == Enum.KeyCode.LeftShift then
           isPressing = false
     end
end)

  1. Conditionals: I covered most of that in part 1 and 2 but with the stamina thing you can just add minor edits and basic math to figure that part out, really isn’t hard.

  2. Main: Cream of the crop, I normally just have a Listener with the Heartbeat event that checks the conditions, and based on their states performs actions with TweenService :wink:

RunService.Heartbeat:Connect(function()
--//Constantly Multiplying the Camera's FOV by the Ratio
Camera.FieldOfView = Camera.FieldOfView * SpeedRatio
    if isPressing then
        if isRunning = false
         local SpeedTween = TweenService:Create(Humanoid, TweenInfo, {WalkSpeed = MaxSpeed})
         SpeedTween:Play()
         isRunning = true
         end
      elseif isPressing == false then
         if isRunning then
            local SpeedTween = TweenService:Create(Humanoid, TweenInfo, {WalkSpeed = DefaultSpeed})
            SpeedTween:Play()
            isRunning = false
         end
     end
end)

That’s mainly how I approach a sprint mechanism, if you wanted to add a UI and a Stamina Value etc etc some simple math can accomplish that but that’s the basis of it.

Hope I helped in some way or another

5 Likes

if you’re just checking if a button is pressed, wouldn’t it be better to change the value of if sprinting than call a function that would do the same thing as what’s in the heartbeat function but without the heartbeat?