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);
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
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
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```
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)
@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.
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.
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.