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

attempt to index field 'Parent' a nil value?

I'm trying to make it that once you hover your mouse in a Player, the tool activates.

mouse = game.Players.LocalPlayer:GetMouse()
me = game.Players.LocalPlayer.Name
tool = game.Players.LocalPlayer.Character:WaitForChild('Sword')

function find_player(str)
	for i,v in pairs (game.Players:GetChildren()) do
		if v.Name ~= me then	
			if v.Character then
				if game.Players:FindFirstChild(str) then
					return true
				end
			end
		end
	end
end


while wait(0.01) do
	if mouse.Target then
		if find_player(mouse.Target.Name) or find_player(mouse.Target.Parent.Name) or find_player(mouse.Target.Parent.Parent.Name) or find_player(mouse.Target.Parent.Parent.Parent.Name) then
			tool:Activate()
		end
	end
end

7 comments

0

if mouse.Target and mouse.Target.Parent then

0

You've got to make sure the parent of target exists before indexing it. Otherwise, you might end up doing nil.Name, or something similar.

0

@yHasteeD, it still throws the same error. (line 20)

0

add another end on line 24? Maybe in correct number of ends?

0

the error is on line 20, it says attempt to index field 'Parent' a nil value. @WideSteal321

0

Just try it.

0

Actually look at the error and try to understand what it means. You're taking the parent of the parent of the parent. Just think about it. Only one of those parents need not exist in order to throw that error.

1 answer

✓ Accepted answer

You're going about it from the wrong direction. The problem is exactly what has been stated in the comments. You're trying to point to objects (ancestors of mouse.Target) that don't exist. For example, mouse.Target might be game.Workspace.Part, so when you try to find mouse.Target.Parent.Parent.Parent, it will throw an error because 'game' doesn't have a Parent.

A different way you could do this though, is get a table of the player's and then check if mouse.Target has a parent with the same name as one of those objects (which ends up being the character)

Something like the following:

local players = game.Players:GetPlayers()

while game:GetService("RunService").Heartbeat:wait() do
	for i, v in pairs(players) do
		local character = mouse.Target:FindFirstAncestor(v.Name)
		if character then
			--code to run
			break
		end
	end
end

Note that with this code you also get a variable named 'character', that if found will point to the character model in Workspace.

Also, I just set this up to run on every frame rendered, but I would highly suggest looking into calling this as a function when the target changes in order to optimize your code. I'll leave you to that part though.

Hope this helps.

3 comments

0

Character is a property not a child so line 5 does nothing

1

What lol

0

That's not true at all. In this case, 'character' is simply a variable that I'm setting to an object. It has absolutely nothing to do with the Character property of a Player instance.

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