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

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

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?

2 comments

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.

0

Whenever I do that, an error fires on line 06 saying "attempt to index a nil value"

1 answer

try using WaitForChild() instead of FindFirstChild

1 comment

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.

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