My local script "attempt to index nil with 'Parent'"

Hello you can call me Tank,

Im pretty new to scripting and I need your help to help me fix my problem for scripting.

Somethings you need to know for context

  • My player is Thetankwargunner2
  • “Chompy” is a tool
  • “Shroom” is a tool
  • WeaponStorage is a folder I created and its in Game.Players.LocalPlayer
  • the problem is on line 10
  • The output saids " [Players.thetankwargunner2.PlayerScripts.WeaponStorage:3: attempt to index nil with ‘FindFirstChild’ "
  • Im also using another script to clone “Chompy” when clicking this UI button
  • Im also using another script to clone “Shroom” when clicking this UI button
  • im using a local script

So basically what im trying to do is if the player has something named Chompy and Shroom in game.Players.LocalPlayer.StarterGear then move it to WeaponStorage which is in game.Players.LocalPlayers

Im not really sure whats the problem with this so I tried using :FindFirstChild instead of :WaitForChild and the output said
“Players.thetankwargunner2.PlayerScripts.WeaponStorage:3: attempt to index nil with ‘FindFirstChild’”

I stated the variables and used :WaitForChild because the item instance has not been replicated yet. I also used a timeOut on the :WaitForChild so it checks it every 5 seconds? I beleive.

local WeaponFolder = script.Parent.Parent
local StarterGearFound = WeaponFolder:WaitForChild("StarterGear", 5)
local ChompyFound = StarterGearFound:WaitForChild("Chompy", 5)
local ShroomFound = StarterGearFound:WaitForChild("Shroom", 5)


-- stuff


ChompyFound.Parent = WeaponFolder.WeaponStorage -- line 10

ShroomFound.Parent = WeaponFolder.WeaponStorage


Just put a wait before your code starts. Also make sure those items actually exist.

You need to put a wait because roblox clones the stuff from StarterGui to the PlayerGui and that takes time.

wait(4)
local WeaponFolder = script.Parent.Parent
local StarterGearFound = WeaponFolder:WaitForChild("StarterGear", 5)
local ChompyFound = StarterGearFound:WaitForChild("Chompy", 5)
local ShroomFound = StarterGearFound:WaitForChild("Shroom", 5)


-- stuff


ChompyFound.Parent = WeaponFolder.WeaponStorage -- line 10

ShroomFound.Parent = WeaponFolder.WeaponStorage

Okay I putted in the wait before the script started like you did… but it just gave me another error on the same line. Also the items do exists which confuses me because I told the script to wait for this item and if its found than move it to this folder.

error in the output

script

wait(5)

local WeaponFolder = script.Parent.Parent

local StarterGearFound = WeaponFolder:WaitForChild("StarterGear", 5)

local ChompyFound = StarterGearFound:WaitForChild("Chompy", 5)

local ShroomFound = StarterGearFound:WaitForChild("Shroom", 5)

-- stuff

ChompyFound.Parent = WeaponFolder.WeaponStorage -- line 10

ShroomFound.Parent = WeaponFolder.WeaponStorage

Can you show the items (the explorer) like this
Screenshot_4
The error basically means that script.Parent.Parent doesn’t exist

Do you want a specific part of the explorer like showing workspace,starterPlayer,ect.
image

Also script.Parent.Parent should be game.Players.LocalPlayer

or maybe the script doesn’t know it exist yet so maybe I have to use a :WaitForChild

If script.Parent.Parent is game.Players.LocalPlayer then you should just reference the local player by game.Players.LocalPlayer.

For me saying script.Parent.Parent is easier to remember for me so I just say that instead. I also swapped out script.Parent.Parent with game.Players.LocalPlayer and it said the same error in the output.

1 Like

just use

repeat wait() until script.Parent.Parent--wait until script.Parent.Parent is true



local WeaponFolder = script.Parent.Parent
local StarterGearFound = WeaponFolder:WaitForChild("StarterGear", 5)
local ChompyFound = StarterGearFound:WaitForChild("Chompy", 5)
local ShroomFound = StarterGearFound:WaitForChild("Shroom", 5)

The solution you just gave did not work unfortunately. Im sure the local script already knows script.Parent.Parent = true
because I did not have any problems with it before regarding that. Also the error message is this still.

If you don’t use a timeout in your WaitForChild, does it ever process through?

I think you need to change

to
local player = game.Players.LocalPlayer
local WeaponFolder = player:WaitForChild(“WeaponFolder”)

The problem here is that these lines doesn’t really wait for Chompy and Shroom.

What’s actually happening is that after checking for the tools in the StarterGearFound, it proceeds to the next line of code, then checks again after 5 seconds.

Obviously, the two variables will be nil since the tools have not loaded yet and it will proceed to this line of code, which causes an error since ChompyFound is still nil.

I recommend that you use the ChildAdded event to detect when tools are cloned to the StarterGear:

local WeaponFolder = script.Parent.Parent
local StarterGearFound = WeaponFolder:WaitForChild("StarterGear")

StarterGearFound.ChildAdded:Connect(function(tool)
    if tool.Name == "Chompy" or tool.Name == "Shroom" then
        tool.Parent = WeaponFolder.WeaponStorage
    end
end)
1 Like

Ah yes! thank you! it didn’t work the first time with your script but with just a few tweaks in the script, it works fine! This pushed me in the right direction of learning something new like :ChildAdded

script if your curious

local WeaponFolder = script.Parent.Parent
local StarterGearFound = WeaponFolder:WaitForChild("StarterGear")

StarterGearFound.ChildAdded:Connect(function(tool)
	if tool.Name == "Chompy" or tool.Name == "Shroom" then -- needed a double **==** for some reason
		wait(1) -- added a wait so it has to wait for the tools to clone into starterGear
		tool.Parent = WeaponFolder.WeaponStorage
	end
end)
1 Like