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

(Answered) Jet Pack works to fly but can't move on X and Z?

Asked by 6 years ago
Edited 6 years ago

I made a jet pack which works to fly when I hold down E, but for some reason when I hold down E it works to fly up but I can't move around using the WASD or Arrow keys? It works to move around when I'm in the air only if I'm not holding down E.

local UIS = game:GetService("UserInputService")
local HoldingE = false
local YVelocity = 0

UIS.InputBegan:Connect(function(inputObject) 
    if(inputObject.KeyCode == Enum.KeyCode.E)then
        HoldingE = true
        while HoldingE do
    YVelocity = YVelocity + 10
    script.Parent.JetPack.Handle.BodyVelocity.MaxForce = Vector3.new(4000, 4000, 4000)
    script.Parent.JetPack.Handle.BodyVelocity.Velocity = Vector3.new(0, YVelocity, 0)
            wait(0.1)
        end

    end
end)
UIS.InputEnded:Connect(function(inputObject)
    if(inputObject.KeyCode == Enum.KeyCode.E)then
        HoldingE = false
        YVelocity = 0
        script.Parent.JetPack.Handle.BodyVelocity.MaxForce = Vector3.new(0, 0, 0)
    end
end)

I've tried removing this part of the script:

    YVelocity = YVelocity + 10
    script.Parent.JetPack.Handle.BodyVelocity.MaxForce = Vector3.new(4000, 4000, 4000)
    script.Parent.JetPack.Handle.BodyVelocity.Velocity = Vector3.new(0, YVelocity, 0)
            wait(0.1)
        end

And when I removed that part I was able to move using the WASD and Arrow keys, so I would assume that the problem is something to do with the body velocity? (I'm New With Body Velocity)

1 answer

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago

The problem is that the BodyVelocity.Velocity property is trying to always set your X and Z velocity to 0.

The solution is to make it so the MaxForce property will not do anything to the X and Z values.

local UIS = game:GetService("UserInputService")
local HoldingE = false
local YVelocity = 0

UIS.InputBegan:Connect(function(inputObject) 
    local handle = script.Parent.JetPack.Handle
    if(inputObject.KeyCode == Enum.KeyCode.E)then
        HoldingE = true
        while HoldingE do
    YVelocity = YVelocity + 10
  handle.BodyVelocity.MaxForce = Vector3.new(0, 4000, 0) -- change max force
    handle.BodyVelocity.Velocity = Vector3.new(0, YVelocity, 0) 
            wait(0.1)
        end

    end
end)
UIS.InputEnded:Connect(function(inputObject)
    local handle = script.Parent.JetPack.Handle
    if(inputObject.KeyCode == Enum.KeyCode.E)then
        HoldingE = false
        YVelocity = 0
        handle.BodyVelocity.MaxForce = Vector3.new(0, 0, 0)
    end
end)

1
Thank You :) kizi3000 88 — 6y
1
np! ihatecars100 502 — 6y
Ad

Answer this question