为什么我的飞行脚本无法正常运行?

时间:2019-01-07 00:42:04

标签: roblox

我正在制作要用于ProtoSmasher的飞行脚本,但该脚本无法正常工作。我希望它有一个切换按钮(G),然后能够通过按钮(W)飞行。取而代之的是,要使其正常工作,我必须按住W键,然后按G键。但是,如果我尝试放开w停止播放,则必须再次按G键。

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character
local hum = char.Humanoid
local Torso = char.HumanoidRootPart
local Mouse = plr:GetMouse()
local toggle = false
local wToggle = false

Mouse.KeyDown:Connect(function(key)
    if key == "w" then
        wToggle = true
    end
    if key == "g" then
        if toggle == false then
            toggle = true
            local BV = Instance.new("BodyVelocity",Torso)
            BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
            while wToggle == true do
                BV.Velocity = Mouse.Hit.lookVector*200
                wait()
            end
        end
        if toggle == true then
            toggle = false
            Torso:FindFirstChildOfClass("BodyVelocity"):remove()
        end
    end
end)

Mouse.KeyUp:Connect(function(key)
    if key == "w" then
        wToggle = false
    end
end)

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试连接键盘按键,但是您是在事件监听器中进行了此操作。

尝试这样的事情:

local isFlying = false

local function onKeyPress(actionName, userInputState, inputObject)
    local isKeyDown = userInputState == Enum.UserInputState.Begin
    local isKeyUp = userInputState == Enum.UserInputState.End

    if actionName == "toggleFlight" then
        if isKeyUp then
            isFlying = not isFlying
        end

    elseif actionName == "fly" then
        if isKeyDown then
            -- start flying forward
        elseif isKeyUp then
            -- stop flying
        end
    end
end

-- listen for keyboard input
game.ContextActionService:BindAction("toggleFlight", onKeyPress, false, Enum.KeyCode.G)
game.ContextActionService:BindAction("fly",          onKeyPress, false, Enum.KeyCode.W)