Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to fix error: attempt to index nil with 'WaitForChild' ?

I need to make sure that when the button is pressed, a pet is issued, but the pet is not displayed but gives an error:attempt to index nil with 'WaitForChild' My script:

script.Parent.MouseButton1Click:Connect(function(Player)

local PetInv = Player:WaitForChild("Pets")

PetInv:WaitForChild(1).Value = PetInv:WaitForChild(1).Value +1

end)

Please, help!

2 comments

0

I updated my answer.

0

Now I write that I have 1 pet out of 40 but it is not visible in the inventory

2 answers

I just noticed the wrong part is that you put Player as an argument in .MouseButton1Click, however, it does NOT have any arguments. Here is your script rewritten:

local player = game:GetService("Players").LocalPlayer

script.Parent.MouseButton1Click:Connect(function()

	local PetInv = player:WaitForChild("Pets")

	PetInv:WaitForChild("1").Value = PetInv:WaitForChild("1").Value + 1

end)

This should definitely work now.

6 comments

0

Doesn't work, has the same error

0

Could you show a picture of the player hierarchy in-game?

0

what ?

0

Like, join the game in studio, open up the player's children and take a screenshot: https://prnt.sc/ul77yp

0

Can I post on Twitter while you take a look?

This is a fixed script, put this as a local script, also if the same error is there, it may be that whatever you are trying to call is not defined. Ex. Did you create a item called "Pets" in the player? If not it will say infinte yield possible. Also, if it says nil, then it doesnt exist, so maybe check your capitalization, and make sure you locate everything correctly. Hope this helps!

local Player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()

	local PetInv = Player:WaitForChild("Pets")

	PetInv:WaitForChild(1).Value = PetInv:WaitForChild(1).Value +1

end)

2 comments

0

Doesn't work

0

Well then it is something wrong else where, are sure everything is defined and you are calling everything correctly

This discussion is preserved as it was. New answers, comments, and votes are closed.