Is this Sprint Script good?

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    This piece of code makes the player sprint whenever they double press W, A, S, or D. I am not satisfied with it, because it doesn’t determine which input the player previously did and reduce the WalkSpeed if it is the same input.
  • What potential improvements have you considered?
    I could attempt to get the last input the player did and see if it was equal to Input.Keycode.
  • How (specifically) do you want to improve the code?
    I want the code to be just overall more convenient and simple to understand.
UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if Input.UserInputType == Enum.UserInputType.Keyboard then
		-- The W, A, S, and D keys.
		if Input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.A or Enum.KeyCode.S or Enum.KeyCode.D then
			print(Input.KeyCode)
			local thisTime = os.clock() - lastTime

			if thisTime > 2 then
				timesPressed = 0
			end

			timesPressed = timesPressed + 1
			lastTime = os.clock()

			if timesPressed == 2 then
				Humanoid.WalkSpeed = Humanoid.WalkSpeed + 10
			end
		end
	end
end)



UserInputService.InputEnded:Connect(function(Input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if Input.UserInputType == Enum.UserInputType.Keyboard then
		if Input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.A or Enum.KeyCode.S or Enum.KeyCode.D then
			if timesPressed == 2 then
				Humanoid.WalkSpeed = Humanoid.WalkSpeed - 10
				timesPressed = 0 -- Resets the times the user has pressed.
			end
		end
	end
end)

Pretty cool cuz my brain cant understand what kind of nuclear formula is this.

1 Like

The current code allows the player to sprint when they double press the W, A, S, or D keys, by increasing the Humanoid.WalkSpeed when two inputs are detected within a certain time. However, the issue arises when the system doesn’t specifically track which key was previously pressed, meaning the sprinting can be triggered even when different keys are pressed consecutively. Additionally, after releasing a key, the WalkSpeed returns to normal, but the logic could be simplified.

Suggested Improvements:

  1. Track Last Key Input: Instead of just counting key presses, you could store the last key pressed and compare it with the current one. This way, sprinting only activates if the player presses the same key twice within the allowed time frame.

  2. Use Timestamps: Store timestamps of when the key was pressed and compare the current time with the last time the same key was pressed to avoid unintended sprint activations.

  3. Simplification and Convenience: You could improve code readability by breaking down the logic into small functions that handle specific tasks like checking double presses and managing sprint activation, which makes it easier to follow and maintain.

local lastKey = nil
local lastPressTime = 0
local sprintSpeed = 16 -- Change this to your desired sprint speed
local defaultSpeed = 8

-- Helper function to check double press
local function checkDoublePress(key)
    local currentTime = os.clock()
    if lastKey == key and (currentTime - lastPressTime) <= 0.5 then
        return true
    else
        lastKey = key
        lastPressTime = currentTime
        return false
    end
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end

    if input.UserInputType == Enum.UserInputType.Keyboard then
        -- Detect W, A, S, D keys
        if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
            if checkDoublePress(input.KeyCode) then
                Humanoid.WalkSpeed = sprintSpeed
            else
                Humanoid.WalkSpeed = defaultSpeed
            end
        end
    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end

    if input.UserInputType == Enum.UserInputType.Keyboard then
        -- Reset WalkSpeed after key release
        if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
            Humanoid.WalkSpeed = defaultSpeed
        end
    end
end)

Benefits of the New Code:

  • Key-Specific Double Presses: The system now checks if the same key was pressed twice within a short timeframe.

  • Cleaner Logic: The separation into a helper function (checkDoublePress) makes it easier to follow and maintain.

1 Like

I made a mistake. We’re trying to make the sprint still carry over. Like let’s say in Deepwoken, when you press W twice, then you press A or another key the walk speed carries over. But here, it’s not the same case.

Sorry. :sweat_smile:

hey, I mean if it works, it works.

1 Like

Nested if statements are not really the most aesthetically pleasig. I would do the input ended like this:

UserInputService.InputEnded:Connect(function(Input, gameProcessedEvent)
	if gameProcessedEvent or Input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if Input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.A or Enum.KeyCode.S or Enum.KeyCode.D and timesPressed == 2 then
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - 10
		timesPressed = 0 -- Resets the times the user has pressed.
	end
end)
1 Like