Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why do I keep getting "attempt to index field 'Parent' (a nil value)"?

This script performs a points on kill reward. Somewhere after the (-3) test i get an 'attempt to index field 'Parent' (a nil value)' error and I have no idea how to fix it.

  StatName = "Points"
  IncreaseValue = 20
	print("-1")--test
local Humanoid = script.Parent.Humanoid
	print("-2")--test
function Killed()
	print("-3")--test
local tag = Humanoid:findFirstChild("creator")
if tag ~= nil then
	print("*4")--test
	if tag.Value ~= nil then
		print("1")--test
		local Leaderstats = tag.Value:findFirstChild("leaderstats")
		if Leaderstats ~= nil then
		Leaderstats[StatName].Value = Leaderstats[StatName].Value + IncreaseValue
		print("2")--test
		wait(0.1)
		print("3")--test
		end

	end

end
end
Humanoid.Died:connect(Killed)

2 comments

0

its failing on line 9

0

Bcause u ar an idiot lol

1 answer

✓ Accepted answer

Humanoid is equal to nil, probably because it isn't created in time. Instead of using FindFirstChild to get the character's Humanoid, try using WaitForChild, which will keep trying to get it until Humanoid does not equal nil.

11 comments

0

I switched it to WaitForChild and still getting the error and a yield warning. Infinite yield possible on 'Workspace.TEST.Humanoid:WaitForChild("creator")'

0

And the error is occurring once I kill the humanoid.

0

When is Killed being called? Also, in the explorer, who is the parent of this script?

0

Killed is being called at the very end. Sorry I forgot to copy that part in but I did update it above. The parent is a model named TEST. The model has a Humanoid in it.

1

It's very important that this script is a sibling of Humanoid. So Humanoid is under TEST, and this script is too? Also, it's case sensitive, so make sure that the name of the Humanoid is "Humanoid".

1

Also, another thing I noticed is that you were doing :findFirstChild in a lot of places instead of :FindFirstChild

1

I made a copy of my character, put the script in it, and printed out the Humanoid variable from the script, and it wasn't equal to nil. So that means that the problem is outside the script.

0

Ok thank you! I will add the script into humanoid and look for all the FindFirstChild errors

0

No, I didn't add it into the humanoid, I added it into the character, which is a model.

0

Fixed it! Thank you so much for all your help!

0

Np

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