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)
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.
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.
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
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:
Follow @serverIess’s advice about the player variable
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.
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.
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.
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
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.
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