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

How do I fix this error?: ServerScriptService.Main:30: attempt to index nil with 'Name

Asked by 3 years ago
Edited 3 years ago

I am trying to make the pet spawn next to you but there is an error when you press the buy button.

This is the scripts it is referring to: Local Script 2:

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

Main:

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new('Folder', player)
    leaderstats.Name = 'leaderstats'

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

    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'
        attachment.Position = Vector3.new(3,3,0)
    end)
end)


game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player, ammount)
    local currency = 'Coins'
    local ammount = 1000
    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.Pets: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()
            clonedPet.Parent = player.Character
            clonedPet:SetPrimaryPartCFrame(player.Character.Head.CFrame)

            local atPet = Instance.new('Attachment', clonedPet.PrimaryPart)

            local ap = Instance.new('AlignPosition')
            ap.Parent = clonedPet
            ap.RigidityEnabled = true
            ap.Attachment0 = atPet
            ap.Attachment1 = player.Character.HumanoidRootPart.CharacterAt

            return 'Bought'
        else
            return 'Not enough coins'
        end
    else
        return 'Equip'
    end
end
0
Can you please elaborate more. And please properly format your code. Soban06 410 — 3y
0
To properly format your code so it is easier to read, just use the lua button above where you type and then type between the ~~~ JustinWe12 723 — 3y
0
I fixed it now please help :) AdamTheBestofThebest 0 — 3y

Answer this question