LuaU 脚本(Roblox),我怎样才能用脚本按下键

Posted

技术标签:

【中文标题】LuaU 脚本(Roblox),我怎样才能用脚本按下键【英文标题】:LuaU script (Roblox), how can I make a Key get pressed with a script 【发布时间】:2021-11-03 14:48:31 【问题描述】:

举个例子

local E = game:GetService('UserInputService').SetKeyDown(Enum.KeyCode.E) 

但它当然不起作用,因为我不能用这个东西让我的游戏自己按 E,所以它需要更长的时间,如果你找到解决方案,你也可以在它按下的地方做一个吗?

【问题讨论】:

【参考方案1】:

输入只能在客户端注册,因此您必须在LocalScript 中编码。有 2 个服务用于获取玩家的输入:-

UserInputService

这个例子展示了如何使用 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

此示例正确地展示了如何使用 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 页面。

【讨论】:

【参考方案2】:

听起来您想编写一个脚本来按下E 键,但这是不可能的。

您可以将操作绑定到按键,就像 Giant427 提供的示例一样,您也可以手动调用绑定到这些操作的函数,但您不能编写脚本来触发键盘输入。

【讨论】:

如果你使用事件是可能的 @GlowBunny、ContextActionService.BindAction、UserInputService.InputBegan、UserInputService.InputChanged 和 UserInputService.InputEnded 都观察键盘状态。但是您不能编写脚本来手动触发这些事件。 没有可绑定事件中的事件,只需在需要时触发它们,当它触发时,运行与按下 e 相同的代码 您正在描述一种可以执行也绑定到按键的功能的方式。最初的问题是询问您将如何编写脚本来触发按键事件本身,大概是用于自动点击器之类的东西。这就是我所说的不可能。

以上是关于LuaU 脚本(Roblox),我怎样才能用脚本按下键的主要内容,如果未能解决你的问题,请参考以下文章

在 ROBLOX 上,我如何制作小游戏脚本? [关闭]

ROBLOX LUAu |获取 DataStore 中的所有键和值

当我购买 DevProduct (Roblox LUA) 时,我怎样才能让事情发生?

用于检测“唯一幸存者”获胜者的 Roblox 游戏脚本

Lua 刷新脚本 (ROBLOX)

脚本超时:已用完允许的执行时间 (ROBLOX)