【问题标题】:Is there a function that executes a script?是否有执行脚本的函数?
【发布时间】:2019-05-20 15:25:04
【问题描述】:

所以,我正在尝试使用 clickdetector 制作用户界面,但我需要运行/调用另一个脚本才能继续。有这样的功能吗?

local clickdetector = game.Workspace.CPU.Part.ClickDetector
clickdetector.MouseClick:Connect(function()

 -- What goes here? 

end)

【问题讨论】:

  • How do I run a .lua file?的可能重复
  • 您可能还需要考虑 1) 将您的其他文件构建为模块,以及 2) 在“沙盒”中执行其他文件。

标签: lua roblox


【解决方案1】:

Roblox 有 3 个脚本概念。 ScriptsLocalScriptsModuleScripts。一个 ModuleScript 被编译一次,结果会返回给任何在其上调用 require() 的脚本。

例如,您可能有一个模块脚本可以为您做一些简单的数学运算。让我们将此脚本称为 MathUtils

local MathUtils = {}
function MathUtils.add(a, b)
    return a + b
end

-- a module script must always return a value
return MathUtils

如果您在 MathUtils 旁边有另一个脚本,您可以像这样包含它:

local MathUtils = require(script.Parent.MathUtils)
print(MathUtils.add(1, 2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    相关资源
    最近更新 更多