Skip to content

Script ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

  • Unit(4 属性 / 25 函数 / 6 事件)

继承成员 ​

2 个来源 / 6 属性 / 25 函数 / 6 事件

Script 是服务端脚本 Unit,继承 BaseScript 的 SourceCode 字段,用于承载只在服务器侧执行的 Lua 逻辑。

适用场景 ​

当需要在场景中创建或检查服务端脚本对象时,可通过 game:CreateUnit("Script", config) 创建后挂到 World 或其他合适父节点;客户端脚本应使用 LocalScript,复用模块逻辑应使用 ModuleScript。

使用要点 ​

创建 Script 后设置 SourceCode,再把 Parent 挂到 World 等运行层级。Script 自身不提供额外函数,常用操作来自 Unit/BaseScript 继承成员。

注意事项 ​

Script 仅在服务器侧执行。SourceCode 和 IsEnabled 是 BaseScript 在 Meta 中公开的继承属性,Icon 是 Script 自身公开的字符串属性;创建和使用时应以这些 Meta 声明为准。

代码示例 ​

创建服务端 Script 并挂到 World ​

lua
-- @runtime server
local World = game:GetService('World')

local scriptUnit = game:CreateUnit('Script', {
    Name = 'ServerHello',
    SourceCode = "print('hello from server script')",
})
if scriptUnit == nil then return end
scriptUnit.Parent = World
print('脚本源码:', scriptUnit.SourceCode)

关联类型 ​