Attempt to index a nil value? Any help?

So I have this script that is supposed to give a hat to this fake character, but it does not work, its giving me a nil value error at 15?

dog = false

script.Parent.MouseButton1Click:connect(function()
local player = script.Parent.Parent.Parent.Parent.Parent
local character = game.Workspace:FindFirstChild("Ignore"):FindFirstChild(player.Name .. "Character")
print("bike found")
dog = true
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 80
player.leaderstats.Hats.Value = 1-- sBike is 5 characters
if player == player then
		h = Instance.new("Accessory")
		p = Instance.new("Part")
		h.Name = "Hat"  
		p.Parent = h
		p.Position = character:FindFirstChild("Head").Position
		p.Name = "Handle" 
		p.formFactor = 0
		p.Size = Vector3.new(-0,-0,-1)
		p.Material = Enum.Material.SmoothPlastic
		p.BrickColor = BrickColor.new("Really black") 
		p.BottomSurface = 0 
		p.TopSurface = 0 
		p.Locked = true 
		script.Parent.Mesh:clone().Parent = p
		h.Parent = character
		print("SUCCESS")
		h.AttachmentPos = Vector3.new(0, -0.20, 0) 
		wait(.1)
		dog = false
		else
if player.leaderstats.Hats.Value == 1 then
end
		end
	end)
6 Likes

are you talking about this line?

2 Likes

If you’re talking about this line then either Head or

is a nil value. Are you certain that is the Character? If it was concatenated it’d be like: ReturnBreakEndCharacter

4 Likes

Yes it is the character. And yes that is the line

You have a fairly odd way of finding characters and more details on your character implementation (supposedly in a folder called Ignore, renamed to PlayerNameCharacter) would be appreciated.

Take the error very literally. Because of the way you’ve set up your code, it’s not possible to tell, but either character is nil or Head is nil. I suggest you start with printing what character is before the line of code that’s supposed to set the position of the accessory part. If it’s nil, your first FindFirstChild is the problem.

I believe the issue is that the character’s model’s Archivable property is set to false. If you set it to true, I think it will fix the issue.

2 Likes

Assuming this is a localscript in a GUI object, you will want to change this at line 4:

local player = script.Parent.Parent.Parent.Parent.Parent

To:

local player = game.Players.LocalPlayer

This is because you can get the player the GUI belongs to by referencing the LocalPlayer, and you can then get their character from that by changing this at line 5:

local character = game.Workspace:FindFirstChild("Ignore"):FindFirstChild(player.Name .. "Character")

To:

local character = player.Character

This is because you would have already made a variable referencing the player that the GUI belongs to, then you will be able to get their character from the player variable.

3 Likes

As @serverIess said change the player variable to that so it is simpler, and like, just in case maybe that is indeed not the character because there is no way that that line is the character unless you parented and named him again, replace that character variable with simply player.Character which would be the character of that player.

local player = game.Players.LocalPlayer
local character = player.Character
2 Likes

As many others have previously said, either character or head is nil on that line.

In order to ensure that character is a correct value:

  1. Follow @serverIess’s advice about the player variable
  2. Change the character variable declaration to the following:
    local character = player.Character or player.CharacterAdded:Wait()

The effect of this is referencing the character if present (I.e loaded already). However, if it is not loaded (AKA nil), it will wait for it to load. From this, you can pretty much guarantee that character will contain a correct value, unless set to nil by something else.

Also, would you mind clarifying if you have any other output in your console? Specifically a warning similar to Infinite yield possible on “Head”. This will help us to narrow down what variable isn’t being found.

The character I’m using is complete different form the actual character, so I cannot do player.character

You usually can, even if you modify the character. Simply modifying the character doesn’t destroy the connection to it via Player.Character.

Can you explain what you mean by “completely different”? Is it still using a model attached to the player?

Unless he’s reparenting the character, no, he can’t use Player.Character. That’s been clarified.

1 Like

Exactly. I know there trying to help, but you know.

2 Likes

Ok, understood. I’m sorry, I didn’t understand what you meant.

Even if you can’t use Player.Character, still change the player variable.

Also, please can you post the full error message ? Along with that, would you mind posting a screenshot of your explorer, with the “Ignore” folder open (so that we can see the character models inside). Make sure you do that when the game is running so that there are characters in there.

1 Like

Have you tried switching FindFirstChild with WaitForChild? Usually “attempt to index nil value” errors happen when something has not loaded yet (If you have your hierarchy set up correctly). If this error still occurs, a screenshot like @CodeNinja16 previously mentioned (twice) would be helpful.

3 Likes

Not necessarily. FindFirstChild returns nil if no instances match the search criteria, which would also lead to the error. WaitForChild would also output an infinite yield warning if it times out, which is why I’ve asked about that too.

There is a certain lack of information on this thread which is making it especially difficult to solve. For us to make progress, @Pooglies please include the following:

  • A screenshot of your console, complete with the error and any other output

  • A screenshot of the explorer structure, specifically the “Ignore” folder when the game is running

  • Print our certain variables (such as the result of the FindFirstChild call on ignore

  • A description of the location of the LocalScript given, if possible the explorer with it visible

4 Likes

I just tried the waitforchild, and its giving me an infinite yield possible error, even though its in the folder, and is named correctly in the error and folder.

1 Like

Change a few things and see if it makes a difference:

  1. Change your player variable to game.Players.LocalPlayer, as suggested a few times. That will sort out any problems with player being incorrect

  2. Change game.Workspace:FindFirstChild(“Ignore”) to just workspace.Ignore. That removes the unnecessary function call, and any errors it causes

  3. Try WaitForChild with all that then come back and tell us what happened.

If possible, please include the mentioned screenshots.

1 Like

local
in front of P and H

Could you show us what the parenting structure looks like?
If you can, when the player is in the correct folder take a picture of the explorer window in studio with the folders open.

On windows you can take a snippet by doing Windows Key + Shift + S

It should look similar to this:
image

2 Likes