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

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

Asked by 3 years ago

I'm making my own game, and I need a pet to be added to the player when the button is pressed. I get an error. Please help me figure it out! My script: script.Parent.MouseButton1Click:connect(function(Player) local PetInv = Player:WaitForChild("Pets") PetInv:WaitForChild(1).Value = PetInv:WaitForChild(1).Value +1 end)

0
Player is nil - Make sure you reference the local player. If you want to give the player tools or anything like that you will need a remote event/function. JesseSong 3916 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

You aren't declaring what "Player" is you need to get the player through game.Players.localplayer (for local scripts) or other means for server scripts (e.g. :GetPlayerFromCharacter)

I'm in mobile at the moment so it's a bit trickier to provide links and useful code but that should get you started! Let me know if it helped :) good luck!

Ad
Log in to vote
-1
Answered by
Coder_1 27
3 years ago

Try this:

local PetInv = Player.Pets PetInv:WaitForChild('1')

script.Parent.MouseButton1Click:Connect(function(Player) 
       PetInv.Value += 1
end)
0
also incorrect JesseSong 3916 — 3y
0
Does not work ZaharKor12 12 — 3y

Answer this question