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

Script works, but outputs "Attempt to index nil with `FindFirstChild`when looking at sky?

The script (localscript) works as intended, but whenever I playtest, it outputs "Attempt to index nil with FindFirstChild, (only when looking at the sky.) This makes it next to impossible to read anything in the output, since it's being occupied by a gazillion of these errors. Here's the script, hope someone can help:

`local player = game.Players.LocalPlayer local mouse = player:GetMouse()

mouse.Move:Connect (function() local target = mouse.Target

if target:FindFirstChild ("dex") then
	target.dex.Enabled = true
	if target.Transparency == 1 then
		target.dex.Enabled = false
	end
elseif target:FindFirstChild ("Berry") then
	target.Berry.dex.Enabled = false
end		

end)`

I'm a rookie at coding, so don't judge if the code's terrible ????

4 comments

0

Whatever target is, it's nil

0

flowery monkey is right. whatever target is, its nil or its an object that doesnt have the function :FindFirstChild()

0

Then how may I formulate the code differently as to prevent these errors??

0

You need to find what is causing target to be nil, where does it originate from? Trace back what could cause it to be nil.

1 answer

the problem is that you not checking if target exists, when u look at the sky, There's nothing (unless theres a part in the sky) so target is nil, to fix it, add another if statement to check if target exists and if it has a parent

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

mouse.Move:Connect (function()
	if mouse.Target then -- check if there's a target else it'll error
		local target = mouse.Target
	end
end

if target and target.Parent then
	if target:FindFirstChild ("dex") then
		target.dex.Enabled = true
		if target.Transparency == 1 then
			target.dex.Enabled = false
		end
		elseif target:FindFirstChild ("Berry") then
			target.Berry.dex.Enabled = false
		end
	end
end

1 comment

0

Wow, it was that easy huh, and you spotted it right away. Thank you so, so much. I appreciate you taking your time to explain a simple problem to a rookie like me. By using your new and improved script, I can actually see the other error messages that I need to focus on. Again, I am ever so grateful. Take care xD!

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