I know far a fact there is a more optimizable way of doing this, But this is the best i can currently think of also cuz i don’t have alot of time inrl.
local userInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
userInputService.InputBegan:Connect(function(input, Gp)
if Gp then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
if Humanoid.MoveDirection.Magnitude > 0 then
while task.wait(1) do
Humanoid.WalkSpeed += 2
if Humanoid.MoveDirection.Magnitude == 0 then break end
end
else
Humanoid.WalkSpeed = 16
end
end
end)
Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
print(Humanoid.WalkSpeed)
if Humanoid.WalkSpeed >= 199 then Humanoid.WalkSpeed = 200 end
end)
As i see i havent implemented every single debounce or boolvalues, but u can easily implement that
It works the way you have it written. If you repeatedly press and release the “e” key the character keeps speeding up. If however you want to just be able to hold the “e” key down and the speed increases without releasing you’d need to add a while loop that does the increase based on a variable say called isEDown that you set to true when you press the “e” key and then to false when you release it.
local isEDown = false
game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
if key == "e" then
print("down")
isEDown = true
if script.Parent.Humanoid.MoveDirection == Vector3.new (0, 0, 0) then
print("starting speed")
script.Parent.Humanoid.WalkSpeed = 16
else
print("Gain momentum")
end
end
end)
game.Players.LocalPlayer:GetMouse().KeyUp:Connect(function(key)
if key == "e" then
print("up")
isEDown = false
end
end)
while task.wait() do
if isEDown then
script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed + .2
if script.Parent.Humanoid.WalkSpeed >= 199 then
script.Parent.Humanoid.WalkSpeed = 200 -- speed cap
print("end")
end
end
end