【问题标题】:Why is my flight script not functioning correctly?为什么我的飞行脚本无法正常运行?
【发布时间】:2019-01-08 08:51:14
【问题描述】:

我正在制作一个用于 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)

【问题讨论】:

  • 如果你想打一次G 然后使用W 飞行,我会将G 设置为仅用于打开或关闭飞行的布尔切换,并将实际飞行代码放入W

标签: roblox


【解决方案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)

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 2015-03-24
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多