Attempt Index Nil

I am very new at scripting. What is wrong with this?


19:59:26.815 - Players.Play_MazeOfHeck.PlayerGui.ScreenGui.Shop.Stats.Frame.LongStick.Buy.Buy:3: attempt to index nil with ‘WaitForChild’

local Cost = 100

  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned.Value 

  if playerGold.Value >= Cost and  Owned.Value == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned.Value = true

1 Like

There are four occurrences of :WaitForChild in your script. Be more specific as to which line.

The first one

Where do you have player defined? or is it not defined. You’re getting that error because player is nil or not defined anywhere.

I think you might’ve cut off your player variable by accident when posting your code. One unfortunate mistake I see in a lot of beginners is that they try to call the LocalPlayer in a server Script. It is only possible to call LocalPlayer from a LocalScript, make sure you did not fall for this.


Also after you get past this error you will need to remove the .Value on this:
local Owned = gui.Shop.Stats.Frame.LongStick.Owned.Value
because you can not change a value from that, it will just give you the value at the time it was defined.

1 Like

It’s in a local script, how do I define player?

local player = game.Players.LocalPlayer