Not working Raycast

So I have a script that saves the enemy who landed on the ground stones, the problem is the result of landing + rock position and params. Сan’t work. The result of this is nil. Here is the part of the script where I do all this

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
						
			local result = workspace:Raycast(part.Position + craterRaycastResult.Normal * 4, craterRaycastResult.Normal * -5, params)
			print(result) -- nil
			if result then
				part.Position = result.Position
				part.Material = result.Material
				part.Color = result.Instance.Color
			else
				part:Destroy()
			end

And if anyone needs the game file (test file)
Combat, test.rbxl (58.3 KB)
I will tell you right away that this script is in the local script at the tool.

I do this in a function that I call in a script, no local. So it should work.

Can you draw what its supposed to look like and where the raycasts you want are supposed to be measured from?

The problem is on line 47 of the CombatServer, the ray’s length is not long enough to reach the ground. I multiplied it by the same amount as the velocity (80 instead of 1) and it worked consistently.

Sorry, actually, I misread. I was looking at the wrong part.

The issue might be with the second raycast (result). Have you tried increasing the length (-5) of the second raycast to better capture the underlying terrain/material data? Alternatively, you could also try modifying the properties in your params to allow raycasting against more layers and/or specific parts that your algorithm may be failing to target.

Another area of concern is the generated random sizes of the Part. If the size is too large when the Part is being rotated and moved, it may move out of the visibility or detection threshold of the second raycast. I would suggest testing this portion of the code by making the part size a fixed value, observing if the issue persists, and iterating from there.

Went back and looked at the local script and realized changing the Ray’s FilterType from “Exclude” to “Include” gave me the rocks every time.

Wait, so I just replace “Excude” with “Include” and that’s it?

Can I have a code example please?

If I replace exclude with include, my output print does not print

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