Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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)
0
its failing on line 9 cooldrewbie 94 — 5y
0
Bcause u ar an idiot lol shinferno -53 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

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.

0
I switched it to WaitForChild and still getting the error and a yield warning. Infinite yield possible on 'Workspace.TEST.Humanoid:WaitForChild("creator")' cooldrewbie 94 — 5y
0
And the error is occurring once I kill the humanoid. cooldrewbie 94 — 5y
0
When is Killed being called? Also, in the explorer, who is the parent of this script? davidgingerich 603 — 5y
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. cooldrewbie 94 — 5y
View all comments (7 more)
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". davidgingerich 603 — 5y
1
Also, another thing I noticed is that you were doing :findFirstChild in a lot of places instead of :FindFirstChild davidgingerich 603 — 5y
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. davidgingerich 603 — 5y
0
Ok thank you! I will add the script into humanoid and look for all the FindFirstChild errors cooldrewbie 94 — 5y
0
No, I didn't add it into the humanoid, I added it into the character, which is a model. davidgingerich 603 — 5y
0
Fixed it! Thank you so much for all your help! cooldrewbie 94 — 5y
0
Np davidgingerich 603 — 5y
Ad

Answer this question