Shift to gain momentum

You can write your topic however you want, but you need to answer these questions:

  1. **What do you want to achieve?
    Im trying to make a shift to run but the longer you run gives you more momentum
  2. **What is the issue?

game.Players.LocalPlayer:GetMouse() .KeyDown:Connect(function(key)
if key == “e” then
print(“down”)
if script.Parent.Humanoid.MoveDirection == Vector3.new (0, 0, 0) then
print(“starting speed”)
script.Parent.Humanoid.WalkSpeed = 16
else
print(“Gain momentum”)
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

end)
game.Players.LocalPlayer:GetMouse().KeyUp:Connect(function(key)
if key == “e” then
print(“up”)
end
end)

Everytime i press e to run the command

	 script.Parent.Humanoid.MoveDirection == Vector3.new (0, 0, 0) 
   script.Parent.Humanoid.WalkSpeed = 16


   script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed + .2
	if script.Parent.Humanoid.WalkSpeed >= 199 then
			script.Parent.Humanoid.WalkSpeed = 200 -- speed cap

doesnt work

Ive tried to use print to find where it stops working and i tried to see why it doesnt work but i cant really see why it wont work?

(“i haven’t changed it to shift to run yet im using the e button temporarily”)

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

1 Like

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
1 Like

Thanks i didnt even notice the mistake :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.