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

why am I getting attempted to index nil with parent?[SOLVED]

Asked by 4 years ago
Edited 4 years ago

I keep getting this error when I click a button twice. here is my code

local List = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.GunNames2.Lists:GetChildren()
local SelectedClass = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.CurrentClassSelected
local ViewPort = script.Parent
local cam = workspace.CurrentCamera
cam.CameraType = "Fixed"
for int1 = 1,#List,1 do
    local ClassGunContainers = List[int1]:GetChildren()
    for int2 = 1,#ClassGunContainers,1 do
        local GunNamesByClass = ClassGunContainers[int2]:GetChildren()
        for int3 = 1,#GunNamesByClass,1 do
            GunNamesByClass[int3].MouseButton1Click:Connect(function()
                local WeaponsAndTools = workspace.WeaponsAndTools
                local SelectedWeapon = WeaponsAndTools:FindFirstChild(tostring(GunNamesByClass[int3]))
                SelectedWeapon.Parent = ViewPort
                print(SelectedWeapon.Parent)
            end)
        end 
    end
end

The error is: Players.mikey2019d.PlayerGui.MainMenu.Screens.ShopFrame.ViewportFrame.ViewPortScript:14: attempt to index nil with 'Parent'

Thanks for taking the time to read this. I apologize if it is a very simple mistake. Thanks. Yes, I know I have another question that is the same as this one. The reason for me asking the same question is because I accidentally said solved when it worked before. Turns out I was on another script without the click. Sorry for the confusion.

Edit: Found my problem as to why I am getting an attempt to index nil with 'Parent'. Now I am getting the output as WeaponsAndTools when it should be ViewportFrame.

Edit: Found my problem I was checking the wrong parent. Sorry for any confusion

this is my edited code if anyone has a similar problem to what I faced

local List = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.GunNames2.Lists:GetChildren()
local SelectedClass = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.CurrentClassSelected
local ViewPort = script.Parent
local cam = workspace.CurrentCamera
cam.CameraType = "Fixed"
for int1 = 1,#List,1 do
    local ClassGunContainers = List[int1]:GetChildren()
    for int2 = 1,#ClassGunContainers,1 do
        local GunNamesByClass = ClassGunContainers[int2]:GetChildren()
        for int3 = 1,#GunNamesByClass,1 do
            GunNamesByClass[int3].MouseButton1Click:Connect(function()
                local WeaponsAndTools = workspace.WeaponsAndTools
                local SelectedWeapon = WeaponsAndTools:FindFirstChild(tostring(GunNamesByClass[int3]))
                --You should put an If statement checking whether the parent is not equal to the parent you are trying to re locate it to. This will fix the Attempted to index nil with parent

                if SelectedWeapon.Parent == WeaponsAndTools then
                SelectedWeapon.Parent = ViewPort
                end
                print(SelectedWeapon.Parent.Name)

            end)
        end 
    end
end

Answer this question