Input 当按下该键时,Roblox Lua将运行脚本,并且距离您一定距离

Input 当按下该键时,Roblox Lua将运行脚本,并且距离您一定距离,input,lua,roblox,Input,Lua,Roblox,我正在尝试用Roblox制作一个脚本,这样,如果你在20个螺柱之外,按下E键,它就会弹出一个NPC对话框。它正在本地脚本中运行。目前,我只是让它在按下E键时显示消息,但它不会显示消息 local HumanoidRootPart = game.Players.LocalPlayer:WaitForChild("HumanoidRootPart") local UserInputService = game:GetService("UserInputService") local part = g

我正在尝试用Roblox制作一个脚本,这样,如果你在20个螺柱之外,按下E键,它就会弹出一个NPC对话框。它正在本地脚本中运行。目前,我只是让它在按下E键时显示消息,但它不会显示消息

local HumanoidRootPart = game.Players.LocalPlayer:WaitForChild("HumanoidRootPart")
local UserInputService = game:GetService("UserInputService")
local part = game.workspace.TableBox.TableTop

UserInputService.InputBegan:connect(function(keyCode)
    if keyCode.keyCode == Enum.KeyCode.E then
        if (part.Position - HumanoidRootPart.Position).magnitude < 20 then
            print("E has been pressed")
        end
    end
end)
我在输出橙色中也得到了这个错误:

“玩家”上可能出现无限收益。icrann:WaitForChildHumanoidRootPart”

我希望local HumanoidRootPart=game.Players.LocalPlayer.Character:WaitForChildHumanoidRootPart可以工作,但它最终会出现错误:

Players.icrann.PlayerScripts.Script:1:尝试将字段“Character”索引为零值

此外,当我玩游戏时,我在explorer中的角色如下所示:


将此替换为HumanoidRootPart

本地玩家=游戏:GetServicePlayer.LocalPlayer 本地角色=玩家。角色或玩家。添加的角色:等待 local HumanoidRootPart=字符:WaitForChildHumanoidRootPart
经过数小时的研究,我发现了一种方法,它并不是100%有效,但几乎可以做到

local HumanoidRootPart = workspace.icrann:WaitForChild("HumanoidRootPart")
local UserInputService = game:GetService("UserInputService")
local part = workspace.TableBox.TableTop

UserInputService.InputBegan:connect(function(keyCode)
    print(HumanoidRootPart.Position)
    if keyCode.keyCode == Enum.KeyCode.E and (part.Position - HumanoidRootPart.Position).magnitude <= 20 then
        print("E has been pressed")
    end
end)

现在唯一的问题是获取玩家的用户名。如果你有任何额外的建议如何做到这一点,请添加评论或回答。感谢所有试图提供帮助的人。

我认为应该是:如果part.Position-HumanoidRootPart.Position.magnity>=20My代码与您的建议配合肯定会更好,然而不幸的是,它并没有修复它。不幸的是,当我尝试这样做时,它会出现一个错误:Players.icrann.PlayerScripts.Script:1:尝试将字段“Character”索引为零value@CrannMoroney我已编辑代码,请重试。