Roblox按键未注册

时间:2019-08-21 06:46:09

标签: lua roblox

我试图在用户按下G键时调用一个函数,但是按键本身并未注册。我在这里使用了print()函数,以确保100%确保我所调用的函数不是问题。

我正在使用的代码在StarterPlayer> StarterCharacterScripts

下的LocalScript中运行
local player = game.Players.LocalPlayer
game:service'UserInputService'.InputBegan:connect(function(inputObject,gameProcessedEvent))
    if (inputObject.KeyCode=='G') then
        print("Key pressed!")
end)

在这种情况下,输出应为“已按下键!”,但“输出”选项卡中没有任何显示。

1 个答案:

答案 0 :(得分:0)

我不知道

中使用的名为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)