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
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
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
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