Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to index nil with 'Name'?

Asked by 3 years ago
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new('Folder', player)
    leaderstats.Name = 'leaderstats'

    local Pet = Instance.new('Folder', player)
    Pet.Name = 'Pet'

    local coins = Instance.new("IntValue", leaderstats)
    coins.Name = 'Coins'
    coins.Value = 0

    player.CharacterAdded:connect(function(char)
        local attachment = Instance.new("Attachment", char.HumanoidRootPart)
        attachment.Name = "CharacterAt"

    end)
end)





game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player)
    local currency = 'Coins'
    local ammount = 1

    player.leaderstats[currency].Value = player.leaderstats[currency].Value +  ammount


end)  

game.ReplicatedStorage.RemoteFunctions.EquipPet.OnServerInvoke = function(player, pet)


    local currency = "Coins"
    local MainPet = game.ServerStorage.Pets:FindFirstChild(pet)

    if not player.Pet:FindFirstChild(MainPet.Name) then
        if player.leaderstats[currency].Value >= MainPet.Price.Value then
            player.leaderstats[currency].Value = player.leaderstats[currency].Value - MainPet.Price.Value
            local clonedPet = MainPet:Clone()
            local atPet = Instance.new("Attachment", clonedPet.PrimaryPart)

            local ap = Instance.new ("AlignPosition")
            ap.Parent = pet 
            ap.RigdigityEnabled = true 
            clonedPet.Parent = player.Character
            clonedPet:SetPrimaryPartCFrame(player.Character.Head.Cframe)
            ap.Attachment0 = atPet
            ap.Attachment1 = player.Character.HumanoidRootPart.CharacterAt




            return "Bought"
        else
            return "Not enough coins"

        end
    else
        return "Equip"
    end
end

Problem error ServerScriptService.Main:38: attempt to index nil with 'Name' - Server - Main:38

ServerScriptService.Main:38: attempt to index nil with 'Name' - Client - LocalScript:2 Local Script

script.Parent.MouseButton1Click:Connect(function()
    local result = game.ReplicatedStorage.RemoteFunctions.EquipPet:InvokeServer(script.Parent.Parent.Name)
end)

Note: The code stops at this line - if not player.Pet:FindFirstChild(MainPet.Name) then

0
don't spam questions Xyternal 247 — 3y
0
to index nil with name means that you are calling name on something that does not exist, since you are calling name on pet it means the pet does not exist, which means the findfirstchild is coming up empty. Surround the logic with if pet~=nil then to make it proof to this case. If it never runs though, that is of course a bigger problem, notably, how you instantiate the pet model. MrGreystone 0 — 3y

Answer this question