How would I make bhopping?

I’m making a GMod type game, I need to make bhopping for it. I already figured it out but it’s kinda wack a little bit. You can’t bhop sideways, or backwards. It only goes forward. So, what I wanna do is make a bhop script that makes the player bhop depending on which key they are holding down. For instance, if they are holding s, they bhop backwards. If they hold A or D, they go left or right, depending on the key. And W, they just go forward. How would I do this?

Script:

repeat
	task.wait();
until game:GetService("Players").LocalPlayer and game:GetService("Players").LocalPlayer.Character;

local Character = game:GetService('Players').LocalPlayer.Character;
local uis = game:GetService("UserInputService");

local bhopped = false;

local function BHop(ActionName, InputState, Input)
	if ActionName == "BHop" then
		if InputState == Enum.UserInputState.Begin then
			if not bhopped then
				bhopped = true;
				Character:WaitForChild("HumanoidRootPart").Velocity = Character:WaitForChild("HumanoidRootPart").CFrame.LookVector * 175 + Vector3.new(0, 50, 0);
				task.wait(0.5);
				bhopped = false;
			end;
		end;
	end;
end;

game:GetService"ContextActionService":BindAction("BHop", BHop, false, Enum.KeyCode.Space);
4 Likes

Well, you’ve already used the lookVector, so going backwards is as simple as subtracting lookVector or inverting it, you can do the same with left and right by using CFrame.rightVector

4 Likes

I will try that, thank you!

(char limit lol)

2 Likes

This has worked, however, it might be a bit janky as this script was very rushed lol. Thanks!

1 Like

no problem! You might be able to remove some boilerplate since it’s only one line that needs changing, but I’m not completely sure what the logistics would be behind that

3 Likes

Possibly, however, I’m a little worried about what happens if you hold w and d at the same time. Or 2 movement keys at the same time.

1 Like

I have a relevant code snippet from what I’m currently working on. What I do is add together all of the different movement key’s corresponding direction vectors, and then normalize that if it doesn’t have a magnitude of 0 (a Vector of (0, 0, 0) cannot be normalized):

		local camRight = Vector2.new(camera.CFrame.XVector.X, camera.CFrame.XVector.Z).Unit
		local camFront = -Vector2.new(camera.CFrame.ZVector.X, camera.CFrame.ZVector.Z).Unit
		
		-- cardinal direction walking for keyboard
		local forceVector = Vector2.zero -- Vector that determines where the player should walk
	
		if UserInputService:IsKeyDown(Enum.KeyCode.W) then
			forceVector += camFront
		end
		
		if UserInputService:IsKeyDown(Enum.KeyCode.S) then
			forceVector -= camFront
		end
		
		if UserInputService:IsKeyDown(Enum.KeyCode.D) then
			forceVector += camRight
		end
		
		if UserInputService:IsKeyDown(Enum.KeyCode.A) then
			forceVector -= camRight
		end
		
		if forceVector.Magnitude > 0 then
			forceVector = forceVector.Unit
		end```
3 Likes

Then you just multiply the players rootpart lookvector by the force vector?

The forceVector is actually the direction the player would be moving in. I simply multiply it by a value to determine how fast they move in the direction afterwards. In this case, you would change the velocity by forceVector * 175 + Vector3.new(0, 50, 0) (in this case, you’d probably want a Vector3 instead of a Vector2, since my sample doesn’t include jumping, hence the Vector2)

4 Likes

Alright! I’ll try it out. I will let you know if it works.

3 Likes

Holy crap, I’m amazed on how it works. It perfectly works like what I wanted it to be. Thank you so much!

1 Like

@Joshument After experimenting with the bhopping for a little bit, it’s kinda buggy. For instance, if I hold S and A at the same time, it goes right sometimes instead of left.

2 Likes

It rarely happens however, so it’s alright.

1 Like

Does it by any chance change depending on whether or not you are looking up or down? If so, you might want to construct new Vectors with the Y axis set to 0 for the lookVector and rightVector.

4 Likes

Yes, I have Y set to 0. Else you would fly all over the place in the air. However, when I hold D I go backwards instead of right sometimes. I don’t know why it’s happening either.

1 Like

I know its been solved but, you should use LinearVelocity to make it a little bit more clean. LinearVelocity (roblox.com)

5 Likes