Hello! I have been trying to make a raycasting gun that checks if it hit a character or the character's held tools. So far I could not find anything about this.
My code:
if hit then
if hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Humanoid.Health > 0 then
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
end
end
end
Why am I trying to see if it hit any of a character's tools?
I want the player to be able to shoot the other player ignoring if the slight part of another gun is blocking it.
How my Tool looks Image/Picture
Error(s)/Stacktrace
12:25:06.349 - ServerScriptService.ServerGun:21: attempt to index field 'Parent' (a nil value)
12:25:06.350 - Stack Begin
12:25:06.350 - Script 'ServerScriptService.ServerGun', Line 21
12:25:06.350 - Stack End
Answer/What I did to solve it: https://pastebin.com/raw/cd2RC2gy

4 comments
I tried adding ~= nil to all of the 'or' checks, it did not work.
no, the thing is, there isnt a parent for the instance hit
Yeah, that is the problem, if there is no instance it will just error thus creating a massive amount of errors in a full game. Although, I did try something similar and it worked. You apparently cannot try to match FindFirstChild() with nil, so I got around that. I will mark your question as an answer as it partially worked after I extended it, thank you for the solution!
For anyone looking for what I did, here you go: https://pastebin.com/raw/cd2RC2gy