You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox Lua孵蛋系统attempt to index nil with 'Name'报错求助

问题原因

报错attempt to index nil with 'Name'触发在客户端脚本第8行newTemplate.Name = pet.Name,核心原因是服务端触发客户端远程事件时,没有将选中的宠物实例作为参数传递给客户端,客户端接收到的pet参数为nil,访问nil的Name属性直接触发报错。
你服务端代码里虽然正确选出了随机宠物、打印了宠物名,但触发HatchEgg远程事件时只传了player参数,完全没把pet对象传过去:

-- 错误写法
game.ReplicatedStorage.HatchEgg:FireClient(player)

除了这个核心报错,你的代码里还有3处会导致后续运行异常的问题:

  • Tween创建代码存在拼写、参数错误:Positon拼写错误、lookVector大小写错误、误将Enum.EasingDirection.Out写成Enum.EasingStyle.Out、Tween目标表内CFrame构造参数拆分错误、创建Tween时重复传入TweenInfo类型而非提前定义好的tweenInfo实例
  • 宠物模块脚本的return petMoudle语句写在代码块外部,模块无法正确返回表结构,会导致服务端调用模块方法时报错
  • 所有宠物模型必须提前设置PrimaryPart,否则访问PrimaryPart.Position时也会触发nil索引错误
修复方案

1. 修正服务端远程事件触发逻辑

将服务端脚本里的触发代码修改为传入pet参数:

-- 修正后
local pet = petMoudle.chooseRandomPet()
print(pet.Name.." selected")
game.ReplicatedStorage.HatchEgg:FireClient(player, pet)

2. 修正客户端Tween相关代码

把客户端里创建Tween的整段逻辑替换为以下正确写法:

local tweenInfo = TweenInfo.new(
    2,
    Enum.EasingStyle.Bounce,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)
local targetCFrame = CFrame.new(
    petClone.PrimaryPart.Position + (petClone.PrimaryPart.CFrame.LookVector * 5) + Vector3.new(0,0.75,0),
    petClone.PrimaryPart.Position
)
local tween  = game:GetService("TweenService"):Create(camera, tweenInfo, {CFrame = targetCFrame})
tween:Play()

3. 修正模块脚本结构

把return petMoudle语句移动到宠物模块脚本的最后一行,放在模块代码块内部,确保模块能正确返回接口表。

4. 检查宠物模型配置

打开ReplicatedStorage下HatchablePets文件夹里的所有宠物模型,逐个给模型设置PrimaryPart(一般选模型的根部件),避免后续访问PrimaryPart时报错。

内容的提问来源于stack exchange,提问作者Robloxscripter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 22:12:52