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
01 | local List = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.GunNames 2. Lists:GetChildren() |
02 | local SelectedClass = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.CurrentClassSelected |
03 | local ViewPort = script.Parent |
04 | local cam = workspace.CurrentCamera |
05 | cam.CameraType = "Fixed" |
06 | for int 1 = 1 ,#List, 1 do |
07 | local ClassGunContainers = List [ int 1 ] :GetChildren() |
08 | for int 2 = 1 ,#ClassGunContainers, 1 do |
09 | local GunNamesByClass = ClassGunContainers [ int 2 ] :GetChildren() |
10 | for int 3 = 1 ,#GunNamesByClass, 1 do |
11 | GunNamesByClass [ int 3 ] .MouseButton 1 Click:Connect( function () |
12 | local WeaponsAndTools = workspace.WeaponsAndTools |
13 | local SelectedWeapon = WeaponsAndTools:FindFirstChild( tostring (GunNamesByClass [ int 3 ] )) |
14 | SelectedWeapon.Parent = ViewPort |
15 | print (SelectedWeapon.Parent) |
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
01 | local List = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.GunNames 2. Lists:GetChildren() |
02 | local SelectedClass = game.Players.LocalPlayer.PlayerGui.MainMenu.Screens.ShopFrame.GunNames.CurrentClassSelected |
03 | local ViewPort = script.Parent |
04 | local cam = workspace.CurrentCamera |
05 | cam.CameraType = "Fixed" |
06 | for int 1 = 1 ,#List, 1 do |
07 | local ClassGunContainers = List [ int 1 ] :GetChildren() |
08 | for int 2 = 1 ,#ClassGunContainers, 1 do |
09 | local GunNamesByClass = ClassGunContainers [ int 2 ] :GetChildren() |
10 | for int 3 = 1 ,#GunNamesByClass, 1 do |
11 | GunNamesByClass [ int 3 ] .MouseButton 1 Click:Connect( function () |
12 | local WeaponsAndTools = workspace.WeaponsAndTools |
13 | local SelectedWeapon = WeaponsAndTools:FindFirstChild( tostring (GunNamesByClass [ int 3 ] )) |
16 | if SelectedWeapon.Parent = = WeaponsAndTools then |
17 | SelectedWeapon.Parent = ViewPort |
19 | print (SelectedWeapon.Parent.Name) |