Roblox Studio宠物脚本Argument 1 missing or nil错误排查
报错根因
Argument 1 missing or nil错误的核心触发原因是客户端调用装备宠物远程事件时未传递宠物名参数,加上代码存在多处拼写不一致、参数漏传问题,共同导致报错。
具体问题清单
- 客户端
basicEggAnimation脚本的宠物装备点击逻辑中,调用game.ReplicatedStorage.equipPet:FireServer()时没有传入pet.Name参数,服务端接收到的petName值为nil,执行FindFirstChild(nil)直接触发报错。 - 多处命名拼写/大小写不统一,会引发连锁nil错误:
- 宠物装备状态值混用
EquipedPet和EquippedPet两种拼写,访问不存在的属性会返回nil - 卸下宠物逻辑中查找对象名错写为
player.Name.."'s name",和实际生成的宠物名player.Name.."'s pet"不匹配 equipPet函数定义要求传入3个参数,实际调用时漏传第三个petName参数- UI装备状态判断用了大写
"EQUIPPED",但实际设置的文本是首字母大写"Equipped",会导致状态切换逻辑失效 - 访问角色相关对象时未做存在性判断,角色未加载完成时会触发报错
- 宠物装备状态值混用
修复方案
1. 修改basicEggAnimation客户端脚本
找到宠物列表按钮点击逻辑块,补全远程事件参数,修正状态判断逻辑:
-- 修正setTemplateEquipped函数,增加传入模板参数 local function setTemplateEquipped(newTemplate) for i,v in pairs(scrollingFrame:GetChildren()) do if v:FindFirstChild("Equipped") then v.Equipped.Text = "UNEQUIPPED" v.Equipped.TextColor3 = Color3.fromRGB(255,0,0) end end newTemplate.Equiped.Text = "Equipped" newTemplate.Equiped.TextColor3 = Color3.fromRGB(0,255,0) end local function addToFrame(pet) local newTemplate = template:Clone() newTemplate.Name = pet.Name newTemplate.petName.Text = pet.Name newTemplate.Parent = scrollingFrame newTemplate.Visible = true local camera = Instance.new("Camera") local newPet = pet:Clone() newPet.Parent = newTemplate.ViewportFrame camera.CFrame = CFrame.new(newPet.PrimaryPart.Position + (newPet.PrimaryPart.CFrame.LookVector * 3), newPet.PrimaryPart.Position) camera.Parent = newTemplate.ViewportFrame newTemplate.ViewportFrame.CurrentCamera = camera buttonConnection[#buttonConnection+1] = newTemplate.MouseButton1Click:Connect(function() -- 修正大小写匹配问题 if newTemplate.Equiped.Text == "Equipped" then game.ReplicatedStorage.unequipPet:FireServer() newTemplate.Equiped.Text = "UNEQUIPPED" newTemplate.Equiped.TextColor3 = Color3.fromRGB(255,0,0) else -- 补上缺失的pet.Name参数 game.ReplicatedStorage.equipPet:FireServer(pet.Name) setTemplateEquipped(newTemplate) end end) end
2. 修改petFollowPlayer服务端脚本
统一命名规范,补全参数,增加空值判断:
local function equipPet(player, pet, petName) print(player) local character = player.Character if pet ~= nil and character ~= nil then -- 修正宠物名拼写错误 if character:FindFirstChild(player.Name.."'s pet") then character[player.Name.."'s pet"]:Destroy() end if character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy() end pet.Name = player.Name.."'s pet" pet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame) local modelSize = pet.PrimaryPart.Size local attachmentCharacter = Instance.new("Attachment") attachmentCharacter.Visible = false attachmentCharacter.Parent = character.HumanoidRootPart attachmentCharacter.Position = Vector3.new(-1,0,1) + modelSize attachmentCharacter.Name = "attachmentCharacter" local attachmentPet = Instance.new("Attachment") attachmentPet.Parent = pet.PrimaryPart local alignPosition = Instance.new("AlignPosition") alignPosition.MaxForce = 25000 alignPosition.Attachment0 = attachmentPet alignPosition.Attachment1 = attachmentCharacter alignPosition.Responsiveness = 25 alignPosition.Parent = pet local alignOrientation = Instance.new("AlignOrientation") alignOrientation.MaxTorque = 25000 alignOrientation.Attachment0= attachmentPet alignOrientation.Attachment1 = attachmentCharacter alignOrientation.Responsiveness = 25 alignOrientation.Parent = pet pet.Parent = character end end game.Players.PlayerAdded:Connect(function(player) -- 统一命名为正确拼写的EquippedPet local equipedPet = Instance.new("StringValue") equipedPet.Name = "EquippedPet" equipedPet.Parent = player equipedPet.Value = "" player.CharacterAdded:Connect(function(char) char:WaitForChild("HumanoidRootPart") if equipedPet.Value ~= "" and game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value) then local clone =game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value):Clone() -- 补全第三个petName参数 equipPet(player,clone, equipedPet.Value) end end) equipedPet.Changed:Connect(function () if equipedPet.Value ~= "" and equipedPet.Value ~= nil then if game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value) then -- 补全第三个petName参数 equipPet(player, game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value):Clone(), equipedPet.Value) end end end) end) game.ReplicatedStorage.equipPet.OnServerEvent:Connect(function(player,petName) -- 增加petName空值判断,从源头拦截nil报错 if not petName then return end local pet = game.ReplicatedStorage.HatchablePets:FindFirstChild(petName) if pet and player:WaitForChild("PetInventory"):FindFirstChild(petName) then player.EquippedPet.Value = petName end end) game.ReplicatedStorage.unequipPet.OnServerEvent:Connect(function(player) player.EquippedPet.Value = "" -- 增加角色存在性判断 if player.Character then if player.Character:FindFirstChild(player.Name.."'s pet") then player.Character[player.Name.."'s pet"]:Destroy() end if player.Character:FindFirstChild("HumanoidRootPart") and player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy() end end end)
内容的提问来源于stack exchange,提问作者Robloxscripter
相关产品推荐
相关产品推荐

