Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a player move left/right for a keybind script?

Asked by 4 years ago

Hello, I know the question was a bit wonky but I have a script here where, if you double jump, you will fly until you double jump again. It can only go forward(keybind W) and backward(keybind S)

Here is the script of the movement.

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.S then -- this is moving backward when you click S
        if Toggle == false then return end
        HumaoidRP.Anchored = false
        if HumaoidRP:FindFirstChildOfClass("BodyVelocity") then
            HumaoidRP:FindFirstChildOfClass("BodyVelocity"):Destroy()
        end
        local Back = Instance.new("BodyVelocity",HumaoidRP)
        Back.Name = "BackMovement"
        Back.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
        local Gyro = Instance.new("BodyGyro",HumaoidRP)
        Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
        Gyro.D = 100
        Gyro.P = 10000
        while Toggle == true do
            Back.Velocity = Mouse.Hit.lookVector*-30 --I found that if the number is a negative, it goes backwards, if it's positive, it moves forward
            Gyro.CFrame = Mouse.Hit
            wait()
        end
    end
end)
UIS.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.S then
        if Toggle == false then return end
        if HumaoidRP:FindFirstChild("BackMovement") then
            HumaoidRP.BackMovement:Destroy()
            HumaoidRP.Anchored = true
            if HumaoidRP:FindFirstChildOfClass("BodyGyro") then
                HumaoidRP:FindFirstChildOfClass("BodyGyro"):Destroy()
            end
        end
    end
end)

My problem is, how can I make it go right/left? I know I need to change the .Velocity = Mouse.Hit.lookVector*-30, but not sure how. I know that it's all math but I'm very unsure. Does anyone know how to make the movement go right/left?

1 answer

Log in to vote
1
Answered by 4 years ago

So, how a body velocity works is it moves the character by how much you told it to, so here's how to move the player to the right, Change this line:

Back.Velocity = Mouse.Hit.lookVector*Vector3.new(30,0,0)

To the right:

Back.Velocity = Mouse.Hit.lookVector*Vector3.new(-30,0,0)

Hope it helps!

0
Ah it did work but it's moving on the x axis of the whole game, not the actual model for some reason, but thank you! Aprilsaurus 48 — 4y
0
hmm, do you have a weld? alexfinger21 341 — 4y
Ad

Answer this question