Search code examples
luarobloxluau

Argument 1 missing or nil error on roblox studio


Hope you are having a good day. I am just here to say I have watched Alvinblox's egg hatching system tutorial and I am currently at part 2. There is a new error called "Argument 1 missing or nil" it is at line 60 in the petFollowPlayer script (local pet = game.ReplicatedStorage.HatchablePets:FindFirstChild(petName)) here is the code

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)
    local equipedPet = Instance.new("StringValue")
    equipedPet.Name = "EquipedPet"
    equipedPet.Parent = player
    equipedPet.Value = ""
    player.CharacterAdded:Connect(function(char)
        if game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value) then
            local clone =game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value):Clone()
            equipPet(player,clone)
        end
    end)
    equipedPet.Changed:Connect(function ()
        if equipedPet.Value ~= nil then
            if game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value) then
                equipPet(player, game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value):Clone())
            
            end
        end
    end)
end)
game.ReplicatedStorage.equipPet.OnServerEvent:Connect(function(player,petName)
    local pet = game.ReplicatedStorage.HatchablePets:FindFirstChild(petName)
    if pet and player.PetInventory:FindFirstChild(petName) then
        player.EquipedPet.Value = petName
    end
end)
game.ReplicatedStorage.unequipPet.OnServerEvent:Connect(function(player)
    player.EquippedPet.Value = ""
    if player.Character:FindFirstChild(player.Name.."'s name") then
        player.Character[player.Name.."'s pet"]:Destroy()
    end
    if player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
        player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()
    end
end)

buyBasicEgg

local cost = 50
local petMoudle = require(game.ServerScriptService.BasicEggModule)
local prompt = workspace["Egg Holder"].Part.ProximityPrompt


prompt.Triggered:Connect(function (player)
    if player.leaderstats.Cash.Value >= cost then
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - cost
        local pet = petMoudle.chooseRandomPet()
        local petval = Instance.new("StringValue")
        petval.Name = pet.Name
        petval.Parent = player.PetInventory
        print(pet.Name.." selected")
        game.ReplicatedStorage.HatchEgg:FireClient(player, pet)
    end
end)

basicEggAnimation

local camera = workspace.Camera
local studio = workspace.Studio
local template = script.Parent.Pets.ScrollingFrame.Template
local scrollingFrame = script.Parent.Pets.ScrollingFrame
local buttonConnection = {}

local function setTemplateEquipped()
    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
    template.Equiped.Text = "Equipped"
    template.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(pet.Name)
            newTemplate.Equiped.Text = "UNEQUIPPED"
            newTemplate.Equiped.TextColor3 = Color3.fromRGB(255,0,0)
        else
            game.ReplicatedStorage.equipPet:FireServer()
            setTemplateEquipped(newTemplate)
        end
    end)
end

game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function (pet)
    addToFrame(pet)
    camera.CameraType = Enum.CameraType.Scriptable
    camera.CFrame = studio.CameraPart.CFrame
    wait(1.5)
    for i = 1,50,1 do
        studio["Egg Mesh"].Size = studio["Egg Mesh"].Size + Vector3.new(0.1,0.1,0.1)
        wait(0.01)
    end
    local explosion = Instance.new("Explosion")
    explosion.BlastRadius = 10
    explosion.BlastPressure = 0
    explosion.Position = studio["Egg Mesh"].Position
    explosion.ExplosionType = Enum.ExplosionType.NoCraters
    explosion.DestroyJointRadiusPercent = 0
    explosion.Parent = studio["Egg Mesh"]
    studio["Egg Mesh"].Transparency = 1
    local petClone = pet:Clone()
    for i,v in pairs(petClone:GetChildren()) do
        if v:IsA("ParticleEmitter") then
            v.Enabled = true
        end
    end 
    petClone:SetPrimaryPartCFrame(CFrame.new(studio["Egg Mesh"].Position, studio.CameraPart.Position))
    petClone.Parent = studio
    petClone.PrimaryPart.Position = Vector3.new(-14.382, -8.112, -605.565)
    petClone.PrimaryPart.Anchored = true
    local tweenInfo = TweenInfo.new(
        2,
        Enum.EasingStyle.Bounce,
        Enum.EasingDirection.Out,
        0,
        false,
        0
    )
    local TweenService = game:GetService("TweenService")
    local targetVector = petClone.PrimaryPart.Position + (petClone.PrimaryPart.CFrame.lookVector * 5) + Vector3.new(0,0.75,0)
    local lookVector = petClone.PrimaryPart.Position
    local targetCframe = CFrame.new(targetVector, lookVector)
    local propsToTween = { CFrame = targetCframe }
    local tween  = TweenService:Create(camera, tweenInfo, propsToTween)
    tween:Play()
    wait(5)
    for i,v in pairs(petClone:GetChildren()) do
        if v:IsA("ParticleEmitter") then
            v.Enabled = false
        end
    end 
    camera.CameraType = Enum.CameraType.Custom
    studio["Egg Mesh"].Transparency = 0
    studio["Egg Mesh"].Size = Vector3.new(4.732, 6, 4.732)
    petClone:Destroy()
end)

If you can help thank you so much it will be very appreciated :)


Solution

  • Looking at the line local pet = game.ReplicatedStorage.HatchablePets:FindFirstChild(petName)

    The error is telling you that, for some reason, petName isn't defined. So looking at how the variable is defined, we need to check how game.ReplicatedStorage.equipPet is called. So looking in the addToFrame local function, you call it like this : game.ReplicatedStorage.equipPet:FireServer(). Your problem is that you haven't provided the pet name at all. So just do the same thing you did with the unequipPet event.

        buttonConnection[#buttonConnection+1] = newTemplate.MouseButton1Click:Connect(function()
            if newTemplate.Equiped.Text == "EQUIPPED" then
                game.ReplicatedStorage.unequipPet:FireServer(pet.Name)
                newTemplate.Equiped.Text = "UNEQUIPPED"
                newTemplate.Equiped.TextColor3 = Color3.fromRGB(255,0,0)
            else
                game.ReplicatedStorage.equipPet:FireServer(pet.Name)
                setTemplateEquipped(newTemplate)
            end
        end)