Raycasting problem

So I have a script that should save rocks under the enemy when he lands on the ground, but the problem is that my raycast shows “nil”. Here is the part of my script which is responsible for the raycast!

local function crater(data)

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {game.Workspace.Map}
	params.FilterType = Enum.RaycastFilterType.Exclude
	

	local currentTime = tick()
	local craterRaycastResult

	repeat
		craterRaycastResult = workspace:Raycast(data.Target.PrimaryPart.Position, data.Direction, params)
		wait()
	until craterRaycastResult ~= nil or tick() - currentTime > 5

	if craterRaycastResult then
		for i = 0, 14 do
			local part = Instance.new("Part", workspace.Fx)
			part.Size = Vector3.new(4, math.random(10, 20)/10, math.random(10, 20)/10)
			part.Anchored = true
			part.CFrame = CFrame.new(craterRaycastResult.Position, craterRaycastResult.Position + craterRaycastResult.Normal)
			part.CFrame = part.CFrame * CFrame.Angles(math.rad(90), math.rad(i * 360/14), 0) * CFrame.new(0, 0, -4 * 2) * CFrame.Angles(math.rad(35), 0, 0)
			part.CanQuery = false
			part.CanCollide = false
			part.CanTouch = false
			
			print(params)
			
			local result = workspace:Raycast(part.Position + craterRaycastResult.Normal * 4, craterRaycastResult.Normal * -5, params)
			--local result = workspace:Raycast(part.Position + craterRaycastResult.Normal * math.rad(40), craterRaycastResult.Normal * math.rad(-50), params) -- BUG
			print(result) -- nil
			if result then
				part.Position = result.Position
				part.Material = result.Material
				part.Color = result.Instance.Color
			else
				part:Destroy()
			end
			

And here’s my output!

can i see what data is being passed through the function? also is the rock moving whilst you’re calling the raycast?

Sure, look, I call this function right here:

   if data.Action == "Crater" then
   	crater(data)
   end
end)

it’s a data:

local data = {
				["Target"] = hitTarg,
				["Character"] = character,
				["Combo"] = combo,
				["Air"] = air,
				["Action"] = "ml",
			}

And this is where I trigger the function when the enemy landsЖ

	elseif data.Air == "Down" then
			for i, v in pairs(data.Target.PrimaryPart:GetChildren()) do
				if v:IsA("BodyMover") then
					v:Destroy()
				end
			end

			local bv = Instance.new("BodyVelocity", data.Target.PrimaryPart)
			bv.Velocity = (data.Character.PrimaryPart.CFrame.LookVector * 1 - Vector3.new(0, 2, 0)) * 80
			bv.MaxForce = Vector3.new(99999, 99999, 99999)
			bv.Name = "Velocity"
			game.Debris:AddItem(bv, .2)
			
			data.Action = "Crater"
			data.Direction = (data.Character.PrimaryPart.CFrame.LookVector * 1 - Vector3.new(0, 2, 0)) * 1
			event:FireAllClients(data)

From what I see in the script, the “params” variable is defined with a FilterDescendantsInstances property equal to {game.Workspace.Map}, which means that only instances that are children of the game.Workspace.Map instance will be hit by the raycast.

Make sure that the object you are trying to hit is indeed a child of the game.Workspace.Map, otherwise, the raycast won’t hit it.

You could try removing the FilterDescendantsInstances property from the params variable, or change it to include more objects in the raycast.

I deleted that line, but it didn’t make any difference.

local params = RaycastParams.new()
	--params.FilterDescendantsInstances = {game.Workspace.Map}
	params.FilterType = Enum.RaycastFilterType.Exclude
	

Could it be that your raycast is too short? try setting the data.Direction to data.Direction * 500

If you are referring to this line:

		craterRaycastResult = workspace:Raycast(data.Target.PrimaryPart.Position, data.Direction, params)

it works anyway, the problem is that result = nil, but craterRaycastResult works

If you need I can give you the game file, I’m testing their combat system so there is nothing but her. Here is the file:
King Piece, Test.rbxl (58.3 KB)

Oh I do apologise I got confused with what line you were talking about.

Are you attempting to get the surface of the object?

You were talking about these lines:

local currentTime = tick()
	local craterRaycastResult

	repeat
		craterRaycastResult = workspace:Raycast(data.Target.PrimaryPart.Position, data.Direction, params)
		wait()
	until craterRaycastResult ~= nil or tick() - currentTime > 5

they work, and I’m talking about these lines:

			local result = workspace:Raycast(part.Position + craterRaycastResult.Normal * 4, craterRaycastResult.Normal * -5, params)

I try to get this:

			local result = workspace:Raycast(part.Position + craterRaycastResult.Normal * 4, craterRaycastResult.Normal * -5, params)