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

01local List = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.GunNames2.Lists:GetChildren()
02local SelectedClass = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.CurrentClassSelected
03local ViewPort = script.Parent
04local cam = workspace.CurrentCamera
05cam.CameraType = "Fixed"
06for int1 = 1,#List,1 do
07    local ClassGunContainers = List[int1]:GetChildren()
08    for int2 = 1,#ClassGunContainers,1 do
09        local GunNamesByClass = ClassGunContainers[int2]:GetChildren()
10        for int3 = 1,#GunNamesByClass,1 do
11            GunNamesByClass[int3].MouseButton1Click:Connect(function()
12                local WeaponsAndTools = workspace.WeaponsAndTools
13                local SelectedWeapon = WeaponsAndTools:FindFirstChild(tostring(GunNamesByClass[int3]))
14                SelectedWeapon.Parent = ViewPort
15                print(SelectedWeapon.Parent)
16            end)
17        end
18    end
19end

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

01local List = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.GunNames2.Lists:GetChildren()
02local SelectedClass = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.CurrentClassSelected
03local ViewPort = script.Parent
04local cam = workspace.CurrentCamera
05cam.CameraType = "Fixed"
06for int1 = 1,#List,1 do
07    local ClassGunContainers = List[int1]:GetChildren()
08    for int2 = 1,#ClassGunContainers,1 do
09        local GunNamesByClass = ClassGunContainers[int2]:GetChildren()
10        for int3 = 1,#GunNamesByClass,1 do
11            GunNamesByClass[int3].MouseButton1Click:Connect(function()
12                local WeaponsAndTools = workspace.WeaponsAndTools
13                local SelectedWeapon = WeaponsAndTools:FindFirstChild(tostring(GunNamesByClass[int3]))
14                --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
15 
View all 24 lines...

Answer this question