Attempt to index nil with 'Parent'

I do not know how to explain what I wanted to do, but here is the script:

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

if mouse.Target.Parent:IsA("Model") then
if mouse.Target.Parent:FindFirstChild("Humanoid") then
if mouse.Target.Parent:FindFirstChild("MakeAngry") then
mouse.Target.Parent.MakeAngry:Fire Server(player)

An error occurs when I aim at the sky, that is, at the void

1 Like

When you aim at the sky, mouse.Target is nil, so add a check to see if mouse.Target exists, which is a simple if mouse.Target then,

And a recommendation, combine the 3 if statements into 1 and use task.wait instead of wait

I haven’t written scripts for so long. Thanks, I did almost the same, but I wrote
mouse.Target.Parent:IsA("Model") and not mouse.Target == nil

The way you did your version won’t work as you expect as not has higher precedence compared to other logical statements like ==, so what yours will do is inverse the boolean of mouse.Target, since nil is falsey, it’ll turn it into a true, false if there’s something in mouse.Target

It’s best to do and mouse.Target in your case, because not giving any condition just checks if it’s truthy or falsey, nil and false are falsey and everything else (to my knowledge) is truthy, so if mouse.Target is nil, it’ll be a false for luau, so no need to directly check that

Ohhh, I read what you said incorrectly, thought you were trying to say that you changed my proposed solution to the way you mentioned that didn’t work, my bad, still haven’t woken up properly :sweat_smile:


Yes, that’s why my method didn’t work

Nothing wrong, we all make mistakes, thanks again. Good luck!