Error, attempt to index nil with 'WaitForChild'

I currently need help fixing a bug I’m having trouble with.

This is a tool script that detects whether or not a player/NPC has been clicked. The code works, it’s just that I keep getting an “attempt to index nil” error. It is something that is bothering me and I’d like help to fix it.

Code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local rs = game:GetService('ReplicatedStorage')
local clickEvent = rs:WaitForChild('ClickEvent')



tool.Activated:Connect(function()

	local model = mouse.Target:FindFirstAncestorOfClass('Model')
	
	if model:WaitForChild("Humanoid") then

		clickEvent:FireServer(model)
		
		
	end

end)

Error:

This error triggers when I’m clicking a model.

Things that I’ve tried doing:

  • Changing WaitForChild to FindFirstChild

  • Trying to use a tag

  • Making a variable for the “model:WaitForChild”

(For this code I don’t want to use “GetPlayerFromCharacter”, it is only to detect NPC’s & Players.)

1 Like

Just add a check to verify that the model exists. FindFirstAncestorOfClass("Model") will return nil if the Target variable does not have an ancestor of the Model class. Also - you should use FindFirstChild to also verify that the model has a Humanoid.

if model ~= nil and model:FindFirstChild("Humanoid") then
   -- Your code
end
4 Likes

here is the fix :
you need to change mouse.Target:FindFirstChil(“Humanoid”) to mouse.Target.Parent:FindFirstChild(“Humanoid”) because mouse.Target it will return the Part that the mouse is Hitting and in your case you need to check if that part is linked to a character or not
so we have to see the parent of the part that the mouse is hitting and check if it has Humanoid and if it did then continue

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local rs = game:GetService('ReplicatedStorage')
local clickEvent = rs:WaitForChild('ClickEvent')

tool.Activated:Connect(function()
	local model = mouse.Target.Parent:FindFirstChild("Humanoid")
	if model then
		clickEvent:FireServer(model)
	end
end)
1 Like

The code does not find the model, but does find the humanoid of the character model (if any is present). I’ve corrected it a bit.

There is also no reason for creating variables, if they are used once and in such a basic script.
You would always want to lower the amount of lines you can if possible. I’ve kept the model variable since it is used more than once.

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local clickEvent = game:GetService('ReplicatedStorage'):WaitForChild('ClickEvent')

script.Parent.Activated:Connect(function()
	local model = mouse.Target.Parent
	if not model:FindFirstChildOfClass("Humanoid") then return end
	clickEvent:FireServer(model)
end)
1 Like

Hello @TortenSkjold,

I appreciate your response, but I don’t believe you added anything new to the script. However, I intentionally kept the script simple and clear because @ekulny might be new to programming. I didn’t want to make too many changes to their code so that they could understand the modifications I made and how it improved the script.

You are correct, I did not add anything game breaking to the script. I just fixed a minor incorrection in how you defined model, since yours defined humanoid and sent it to the server. :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.