Raycasting result returns nil?

Hello. I am making a gun, and I started making the client modifiers. But, it errors :“Attempt to index nil with Instance”, (Meaning the ray cast result is nil). Here is my code:

local tool = script.Parent;
local isShooting = false;
local mouse = game.Players.LocalPlayer:GetMouse();

function activated()
	isShooting = true;
end;

function de_Activated()
	isShooting = false;
end;

tool.Activated:Connect(activated)
tool.Deactivated:Connect(de_Activated)

while wait(.25) do
	if isShooting then
		local rayCastParams = RaycastParams.new();
		rayCastParams.FilterDescendantsInstances = {script.Parent.Parent, script.Parent}
		rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayCastParams.IgnoreWater = true
		local result = workspace:Raycast(script.Parent.Handle.Position, mouse.Hit.p, rayCastParams);
		if result.Instance then
			print("Object in ray")
		end
	else
	end
end
1 Like

The arguments are start and direction, not start and end.
script.Parent.Handle.Position, mouse.Hit.p-script.Parent.Handle.Position

Still shows up the same error. It works for the first few times, but a bit after it errors that.

Oh oh I see. result returns nil if you didn’t hit anything.
if result then

For some reason it still doesn’t work.
I used it this time:

local tool = script.Parent;
local isShooting = false;
local mouse = game.Players.LocalPlayer:GetMouse();

function activated()
	isShooting = true;
end;

function de_Activated()
	isShooting = false;
end;

tool.Activated:Connect(activated)
tool.Deactivated:Connect(de_Activated)

while wait(.25) do
	if isShooting then
		local rayCastParams = RaycastParams.new();
		rayCastParams.FilterDescendantsInstances = {script.Parent.Parent, script.Parent}
		rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayCastParams.IgnoreWater = true
		local result = workspace:Raycast(script.Parent.Handle.Position, mouse.Hit.p, rayCastParams);
		if not result then return end
		if result.Instance then
			print("Object in ray")
		end
	else
	end
end

I checked:

if not result then return end

Try this.

local tool = script.Parent;
local isShooting = false;
local mouse = game.Players.LocalPlayer:GetMouse();

function activated()
	isShooting = true;
end;

function de_Activated()
	isShooting = false;
end;

tool.Activated:Connect(activated)
tool.Deactivated:Connect(de_Activated)

while wait(.25) do
	if isShooting then
		local rayCastParams = RaycastParams.new();
		rayCastParams.FilterDescendantsInstances = {script.Parent.Parent, script.Parent}
		rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayCastParams.IgnoreWater = true
		local result = workspace:Raycast(script.Parent.Handle.Position, mouse.Hit.p - script.Parent.Handle.Position, rayCastParams);
		if not result then return end
		if result.Instance then
			print("Object in ray")
		end
	else
	end
end

Still the same error. Maybe it is because I send requests to raycast every .25 seconds?

i am very certain that the issue is because if the ray hits nothing, the raycast has no result so it will come out as nil. so there is no RayCastResult.Instance if it hits nothing

instead of doing if result.Instance then do if result then

Change the direction to this:

(mouse.Hit.Position-script.Parent.Handle.Position).Unit * 250 -- your range here
5 Likes