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

attempt to index local 'target' (a nil value)?

Asked by 6 years ago
Edited 6 years ago
handle.Touched:connect(function(hit)
        if not pause2 then
            pause2 = true
            if Mouse.Button1Down then
                if humanoid and hit and not hit:isDescendantOf(person) and Mouse.Button1Down then
                    local target = hit.Parent:FindFirstChild("Humanoid")
                    print("Target variable added.")
                        target:TakeDamage(damage)
                        print("Target took damage!")
                        wait(0.7)
                        pause2 = false
                        print("Hit Function Finished!")
                        else
                    end
                end
            end
        end)

I can't really figure out why this is happening. The error happens on the line that says "target:TakeDamage(damage)". Any help?

0
You should rather use hit:FindFirstAncestorOfClass("Model").Humanoid because if for example an accessory's part fired the event, you would be looking for the Humanoid under an accessory. Amiaa16 3227 — 6y
0
Whenever I do that, an error fires on line 06 saying "attempt to index a nil value" Starflyerz 44 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

try using WaitForChild() instead of FindFirstChild

0
I tried using WaitForChild, but it didn't find the humanoid, even though the part's parent I was touching, had a humanoid in it. Starflyerz 44 — 6y
Ad

Answer this question