lua - LuaU 脚本(Roblox),我怎样才能用脚本按下键
问题描述
一个例子就像
local E = game:GetService('UserInputService').SetKeyDown(Enum.KeyCode.E)
但它当然不起作用,因为我不能用这个东西让我的游戏自己按 E,所以它需要更长的时间,如果你找到解决方案,你也可以做一个让它按下的地方吗?
解决方案
输入只能在客户端上注册,因此您必须在LocalScript
. 有 2 个服务用于获取玩家的输入:-
这个例子展示了如何使用 UserInputService 来获取玩家的 LeftMouseButton 输入。
local UserInputService = game:GetService("UserInputService")
local function onInputBegan(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed!")
end
end
UserInputService.InputBegan:Connect(onInputBegan)
此示例正确显示了如何使用 ContextActionService 将用户输入绑定到上下文操作。上下文是装备的工具;行动是重新加载一些武器。
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function ()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
您应该查看 Wiki 页面。
推荐阅读
- speech-recognition - 使用 Amazon Polly Text to Speech 从音频文件中模仿真实语音波形的语调
- javascript - 如何更改 HTML 中搜索词的颜色?
- kubernetes - 从 terraform 运行 kubernetes 构建
- python - Pytorch RNN 总是为多元时间序列提供相同的输出
- json - 从 API 加载 JSON 数据时“无法解码任何 JSON 对象”
- javascript - 在同一登录表单中显示错误消息的最佳方法是什么
- ios - Xcode 10:图像集“customloading”引用文件“mygif.gif”,但该文件没有有效的扩展名
- c++ - Xcode 中的错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0xe8)
- vba - 更改 MS Word 宏中选定文本的字体
- twitter-bootstrap - 引导响应中的长文章问题