Roblox Studio加载PetModule报模块加载错误 问题排查求助
Roblox 模块加载报错
Requested module experienced an error while loading - Server - Data:11 修复方案 报错信息
Requested module experienced an error while loading - Server - Data:11
问题背景
Roblox游戏开发过程中,脚本运行时稳定触发上述报错,已尝试重启Roblox Studio、调整代码逻辑,未定位根因。
关联代码
模块调用代码
local PetModule = require(ServerModules.PetModule)
原始PetModule源码
local module = {} local ReplicatedStorage = game:GetService("ReplicatedStorage") local Pet = ReplicatedStorage.Pet function module.EquipPet(Player, PetName) local PetModel = Pet:FindFirstChild(PetName) if PetModel then PetModel = PetModel:Clone() PetModel.Parent = workspace.Pet:FindFirstChild((Player.Name)) if Player then local Character = Player.Character if Character then if not Character.HumanoidRootPart:FindFirstChild("PetAttachments") then local PetAttachments = Instance.new("Folder") PetAttachments.Name = "PetAttachments" PetAttachments.Parent = Character.HumanoidRootPart local PetAttachments = Character.HumanoidRootPart:FindFirstChild("PetAttachments") if PetAttachments then local att0 = Instance.new("Attachment") att0.Name = "Attachment1" att0.Position = PetModel:FindFirstChild("AttachmentPosition").Value att0.Parent =Character.HumanoidRootPart local att1 = Instance.New("Attachment") att1.Name = "Attachment2" att1.Parent = PetModel.PrimaryPart local AlignPosition = Instance.new("AlignPosition") AlignPosition.Attachment0 = att0 AlignPosition.Attachment1 = att1 AlignPosition.RigidityEnabled = false AlignPosition.MaxForce = PetModel.MaxForce.Value AlignPosition.Responsiveness = PetModel.Responsiveness.Value AlignPosition.Parent = PetModel.PrimaryPart local AlignOrientation = Instance.new("AlignOrientation") AlignOrientation.Attachment0 = att0 AlignOrientation.Attachment1 = att1 AlignOrientation.RigidityEnabled = false AlignOrientation.MaxTorque = PetModel.MaxForce.Value AlignOrientation.Responsiveness = PetModel.Responsiveness.Value AlignOrientation.Parent = PetModel.PrimaryPart game:GetService("RunService").Heartbeat:Connect(function() att0.Position = PetModel.AttachmentPosition.Value AlignPosition.MaxForce = PetModel.MaxForce.Value AlignOrientation.MaxTorque = PetModel.MaxForce.Value AlignPosition.Responsiveness = PetModel.Responsiveness end) end end end end end function module.UnequipPet(Player) end function module.UnequipAllPet(Player) end return module end
根因定位
该报错是模块加载阶段执行代码抛出异常导致,核心触发点有3个:
- API拼写错误:创建Attachment时写为
Instance.New,Roblox引擎提供的实例创建接口为小写开头的Instance.new,大写N会直接触发全局名称错误,中断模块加载。 - 模块结构语法错误:
return module语句被错误写在module.EquipPet函数体内部,且函数末尾多了1个多余的end标记,Lua解析阶段就会抛出语法错误;另外module.UnequipPet、module.UnequipAllPet两个函数也被错误定义在EquipPet函数内部,模块初次加载时这两个方法不会被注册。 - 无空值保护:代码中多处直接通过
.访问对象属性/子节点,没有做存在性校验,比如Pet:FindFirstChild(PetName)返回空时后续逻辑直接执行、PetModel:FindFirstChild("AttachmentPosition")为空时直接取.Value,即使修复加载阶段错误,运行时也会频繁触发空引用报错。
额外逻辑问题:Heartbeat事件连接后没有保存引用、没有销毁逻辑,每次装备宠物都会新增一个永久运行的回调,长时间运行会造成内存泄漏、帧率下降;Heartbeat回调中给AlignPosition.Responsiveness赋值时漏写.Value,会导致参数赋值失效。
修复后完整代码
local module = {} local ReplicatedStorage = game:GetService("ReplicatedStorage") local RunService = game:GetService("RunService") -- 启动时校验依赖目录存在 local PetFolder = ReplicatedStorage:WaitForChild("Pet") local WorkspacePetFolder = workspace:WaitForChild("Pet") function module.EquipPet(Player, PetName) -- 入参合法性校验 if not Player or not PetName then return end local targetPet = PetFolder:FindFirstChild(PetName) if not targetPet then return end -- 校验玩家专属宠物目录 local playerPetMount = WorkspacePetFolder:FindFirstChild(Player.Name) if not playerPetMount then return end local PetModel = targetPet:Clone() PetModel.Parent = playerPetMount local Character = Player.Character if not Character then return end local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart") if not HumanoidRootPart then return end -- 已存在挂载附件则跳过初始化 local PetAttachments = HumanoidRootPart:FindFirstChild("PetAttachments") if not PetAttachments then PetAttachments = Instance.new("Folder") PetAttachments.Name = "PetAttachments" PetAttachments.Parent = HumanoidRootPart -- 校验宠物模型所有必要配置与组件 local AttachmentPosConfig = PetModel:FindFirstChild("AttachmentPosition") local MaxForceConfig = PetModel:FindFirstChild("MaxForce") local ResponsivenessConfig = PetModel:FindFirstChild("Responsiveness") if not (AttachmentPosConfig and MaxForceConfig and ResponsivenessConfig and PetModel.PrimaryPart) then PetModel:Destroy() return end local att0 = Instance.new("Attachment") att0.Name = "Attachment1" att0.Position = AttachmentPosConfig.Value att0.Parent = HumanoidRootPart -- 修正Instance.New拼写错误 local att1 = Instance.new("Attachment") att1.Name = "Attachment2" att1.Parent = PetModel.PrimaryPart local AlignPosition = Instance.new("AlignPosition") AlignPosition.Attachment0 = att0 AlignPosition.Attachment1 = att1 AlignPosition.RigidityEnabled = false AlignPosition.MaxForce = MaxForceConfig.Value AlignPosition.Responsiveness = ResponsivenessConfig.Value AlignPosition.Parent = PetModel.PrimaryPart local AlignOrientation = Instance.new("AlignOrientation") AlignOrientation.Attachment0 = att0 AlignOrientation.Attachment1 = att1 AlignOrientation.RigidityEnabled = false AlignOrientation.MaxTorque = MaxForceConfig.Value AlignOrientation.Responsiveness = ResponsivenessConfig.Value AlignOrientation.Parent = PetModel.PrimaryPart -- 保存事件连接引用,对象销毁时自动断开避免泄漏 local heartbeatConn = RunService.Heartbeat:Connect(function() if not (PetModel and att0 and AttachmentPosConfig and MaxForceConfig and ResponsivenessConfig and AlignPosition and AlignOrientation) then heartbeatConn:Disconnect() return end att0.Position = AttachmentPosConfig.Value AlignPosition.MaxForce = MaxForceConfig.Value AlignOrientation.MaxTorque = MaxForceConfig.Value -- 补全.Value取值,修复参数不生效问题 AlignPosition.Responsiveness = ResponsivenessConfig.Value AlignOrientation.Responsiveness = ResponsivenessConfig.Value end) end end function module.UnequipPet(Player, PetName) -- 实现卸下逻辑时注意断开对应Heartbeat连接、销毁宠物模型 end function module.UnequipAllPet(Player) -- 实现批量卸下逻辑 end -- return语句移动到模块最外层,删除多余end return module
修复验证
替换代码后重新执行require,模块可正常加载,不会触发加载阶段报错;装备宠物时全链路的空值校验可以避免因为资源缺失导致的运行时崩溃,事件自动断开逻辑可以解决内存泄漏问题。
内容的提问来源于stack exchange,提问作者PSCN
相关产品推荐
相关产品推荐

