Raycast.Instance breaks when fired at sky

Basically this line of code when i fire a raycast at the sky errors with “attempt to index nil wit Instance”

if Raycast.Instance ~= nil then
			MakeParticleFX(Raycast.Position, Raycast.Normal)
			local Hit:Part = Raycast.Instance
			local Humanoid = (Hit.Parent:FindFirstChildOfClass("Humanoid") or Hit.Parent.Parent:FindFirstChildOfClass("Humanoid"))

			if Humanoid and Humanoid.Parent ~= Player.Character then
				Humanoid:TakeDamage(Settings.DAMAGE)
			end
		end

Does anyone know a fix to this?

Since you hit nothing, there is no Raycast.Instance provided, the only way to check if a Raycast hit something is to check if its not equal to nil

if Raycast ~= nil then
			MakeParticleFX(Raycast.Position, Raycast.Normal)
			local Hit:Part = Raycast.Instance
			local Humanoid = (Hit.Parent:FindFirstChildOfClass("Humanoid") or Hit.Parent.Parent:FindFirstChildOfClass("Humanoid"))

			if Humanoid and Humanoid.Parent ~= Player.Character then
				Humanoid:TakeDamage(Settings.DAMAGE)
			end
		end
1 Like

Thank you very much my gun now works!

1 Like

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