Is there any way to make this sprinting script shorter?

Hello, I have a sprint system in my game where if you hold T, you get faster and your stamina will decrease. Once you stop holding T and run out of stamina you will exponentially lose the WalkSpeed bonus and your stamina will regenerate.

Is there any way I could make this script shorter or more efficient? Thank you for any answers!

			repeat task.wait(0.0425)
				if Humanoid.WalkSpeed > 0 then
					Stamina -= StaminaDeduction
					Humanoid.WalkSpeed += 0.65
					local Progress = (Stamina / MaxStamina)
					TweenService:Create(Bar, StaminaTweenInfo, {Size = UDim2.fromScale(Progress, 1)}):Play()
					Bar.Parent.Amount.Text = math.floor(Progress * 100)..'%'
				end
			until not UserInputService:IsKeyDown(Enum.KeyCode.T) or Stamina <= 0 or Jumping
			task.wait(0.3)
			repeat task.wait(0.065)
				Stamina += StaminaDeduction
				local Progress = (Stamina / MaxStamina)
				TweenService:Create(Bar, StaminaTweenInfo, {Size = UDim2.fromScale(Progress, 1)}):Play()
				Bar.Parent.Amount.Text = math.floor(Progress * 100)..'%'
				if Humanoid.WalkSpeed > 0 then Humanoid.WalkSpeed -= 0.65
					if Humanoid.WalkSpeed < 16 then Humanoid.WalkSpeed = 16 end
				end
			until Stamina == 10
			if Stamina > 10 then Stamina = 10 end

Declare this at the top of the script for efficiency.

1 Like

This isn’t really shorter or more efficient but this seems to be in a LocalScript but shouldn’t this use RemoteEvents so the user can’t modify their sprint?

Stamina -= StaminaDeduction
					Humanoid.WalkSpeed += 0.65
					local Progress = (Stamina / MaxStamina)
					TweenService:Create(Bar, StaminaTweenInfo, {Size = UDim2.fromScale(Progress, 1)}):Play()
					Bar.Parent.Amount.Text = math.floor(Progress * 100)..'%'

Since this code is essentially the same, you should put it in a function at the top. You could pass a parameter to dictate whether to add or subtract the walking speed.