Roblox按键无法响应

我正在尝试在用户按下G键时调用一个函数,但按键本身并没有响应。我在这里使用print()函数来确保调用的函数没有问题。

我使用的代码正在StarterPlayer>StarterCharacterScripts下的LocalScript中运行。

local player = game.Players.LocalPlayer
game:service'UserInputService'.InputBegan:connect(function(inputObject,gameProcessedEvent))
    if (inputObject.KeyCode=='G') then
        print("按键已按下!")
end)

在这种情况下,输出应该是"按键已按下!",但输出选项卡中没有任何内容。

点赞
用户2858170
用户2858170

我不知道 service 函数是什么,它在下面的代码中被使用:

game:service'UserInputService'.InputBegan:connect(function(inputObject,gameProcessedEvent))
    if (inputObject.KeyCode=='G') then
        print("Key pressed!")
end)

通常,服务是通过 GetService 获取的。

尝试使用以下代码:

game:GetService('UserInputService').InputBegan:connect(function(inputObject,gameProcessedEvent))
        if (inputObject.KeyCode=='G') then
            print("Key pressed!")
    end)
2019-08-21 07:42:02