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'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)

1 comment

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.

2 answers

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!

Try this:

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

script.Parent.MouseButton1Click:Connect(function(Player) 
       PetInv.Value += 1
end)

2 comments

0

also incorrect

0

Does not work

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