Attempt to index nil with 'Destroy' - Raycast Returns Nil

You can write your topic however you want, but you need to answer these questions:

Hello Developers,

I have a issue with Raycasting Returning a nil value . I tried looking for a solution on the Devforum. Could anyone assist me Please!

  • Here is the Module script :
			if closetItem then
				local primaryPart = Player.Character.PrimaryPart
				local Parts =  game.Workspace.Items:GetDescendants()
				print(Parts)
				warn(Magnitude," "..Object.Name)
				if ClosestDis <= 10 then
					warn("Beep Boop "..Object.Name)
					wait()

					local ray = Ray.new(primaryPart.Position, (closetTreasure.Position - primaryPart.Position).Unit * primaryPart.Radius.Position)
					local hitPart, hitPosition = game.Workspace:FindPartOnRayWithWhitelist(ray, Parts, true )
					warn(hitPart)
					print(game.Workspace.Items:GetDescendants())
					
					local S, E = pcall(function()
						hitPart:Destroy()
					end)
					
					if S then
						print("Deleted Yetted")
						print(S)
					elseif E then
						print(E)
					end
				end

The Module Script is Required in a local script.

Check your whitelist on the Raycast to make sure it’s correct, and I would also recommend switching to workspace:Raycast because ray.New is deprecate

using game.workspace:Raycast ---- Gives Error(Unable to cast bool to RaycastParams)

It doesn’t work the same way as ray.New origin, direction, params