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 field 'Parent' (a nil value) - How do I fix?

23:00:32.202 - Workspace.Model.Scan.Scan.Script:40: attempt to index field 'Parent' (a nil value)
23:00:32.203 - Stack Begin
23:00:32.203 - Script 'Workspace.Model.Scan.Scan.Script', Line 40
23:00:32.203 - Stack End

Here's the part of the code, it works by a player touching a brick.

Door = script.Parent

Inf = "Red Spawn"

Cantouch = onTouched == true

function onTouched(hit) 
		Cantouch = false
		print("Scan hit") 
        local human = hit.Parent:FindFirstChild("Humanoid") 
		script.parent.parent.Display.SurfaceGui.NAME.Text = hit.Parent.Name
        if (human ~= nil ) then
			    if game.Players:playerFromCharacter(hit.Parent).TeamColor == game.Teams:findFirstChild(Inf).TeamColor then
					script.parent.Parent.Display.SurfaceGui.Infected:play()
					script.parent.Parent.Infected.Transparency = 0
					script.Parent.Parent.Display.SurfaceGui.INFECTED.Visible = true
					Cantouch = false
				end

5 comments

0

either "hit" is "game" or its nil

0

Do you have more to share? The problem seems to lie outside of this one line as 'hit' doesn't have a parent.

0

Please post the (whole) script in your answer so that we may better assist you

0

Give us more lines of code

0

Sorry, updated it

1 answer

Well, until you actually put the full script up, I will give my suggestion on how to fix this. Since the error is found on Line 40 you only need to edit this line in your code.

Since the error is that the parent is nil, this means you most likely have too many .Parent in the line, unless the variable was predefined, in which case the error is actually located on the line of the local variable.

The easiest solution to try is to just start removing .Parent from the line until it isn't nil. Please check your parenting of whichever object is being called.

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