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

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

Asked by 3 years ago
Edited 3 years ago

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!

0
I updated my answer. User#32819 0 — 3y
0
Now I write that I have 1 pet out of 40 but it is not visible in the inventory ZaharKor12 12 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

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.

0
Doesn't work, has the same error ZaharKor12 12 — 3y
0
Could you show a picture of the player hierarchy in-game? User#32819 0 — 3y
0
what ? ZaharKor12 12 — 3y
0
Like, join the game in studio, open up the player's children and take a screenshot: https://prnt.sc/ul77yp User#32819 0 — 3y
View all comments (2 more)
0
Can I post on Twitter while you take a look? ZaharKor12 12 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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)
0
Doesn't work ZaharKor12 12 — 3y
0
Well then it is something wrong else where, are sure everything is defined and you are calling everything correctly iamtryingtofindname 22 — 3y

Answer this question