Raycasting issue not detecting a part


I’m attempting to fire a ray down from a height above a selected spawn location but it’s ignoring the red part and casting straight through to the spawnpoint. Then it just spawns the part. I’ve tried checking for instance.Name of the part it hits but it jsut says SpawnArea.

I’m obviously missing something as I’ve tried both excluding all the parts in the StartArea and including only parts in the WorldAssets folder, even the SpawnArea itself but to no avail. It just seems to spawn parts where it wants to.

local function getPosition(): Vector3
	local maxAttempts = 10
	local spawnPoint
	
	for i = 1, maxAttempts do
		local randomX = spawnArea.Position.X + math.random(-spawnArea.Size.X / 2, spawnArea.Size.X / 2)
		local randomY = spawnArea.Position.Y
		local randomZ = spawnArea.Position.Z + math.random(-spawnArea.Size.Z / 2, spawnArea.Size.Z / 2)
		
		spawnPoint = Vector3.new(randomX, randomY, randomZ)
		
		local origin = spawnPoint + Vector3.new(0, 1000, 0) --100 studs above the ground
		local destination = spawnPoint
		local direction = destination - origin
		
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {spawnArea.Parent}
		params.FilterType = Enum.RaycastFilterType.Include
		params.CollisionGroup = "Raycast"
		
		local raycastResult = workspace:Raycast(origin, direction, params)
		print(raycastResult.Instance.Name)
		if not raycastResult or raycastResult.Instance.Name ~= spawnArea.Name then
			print("Not correct result, trying again")
			raycastResult = nil
			task.wait()
			getPosition()
		else
			--print(raycastResult.Instance.Name)
			spawnPoint = Vector3.new(raycastResult.Position.X, raycastResult.Position.Y, raycastResult.Position.Z)
			return spawnPoint
		end
	end
	
	return Vector3.new(0, 5, 0) --if broken spawn here
end

Is the red, CanQuery = true? Any debugging?


yes.

Ohhhhh u it shouldve been not include but instead the opposite

Or just dont put filterdescendants

Also, your collision group isnt raycast


When I do that I get this error

Bro ur red part isnt in the raycast collision group

Yeah only the SpawnArea has that particular CollisionGroup. It was an attempt to filter out any groups not “Raycast”.

Then just completely take out filter in params, using collectionservice and tags is a better way

I had not considered CS for raycasting. Do you have an example on it’s structure?

Filtertype=include
Filterdescendant=cs.GetTagged(“raycast”)

I just removed the params as you suggested. Works now, I guess I need to read more into how params works but thanks for that and this CS filter example. You’re a legend.

1 Like

Mark as solution if fixed. (if i deserve)

Yep I did. Why must I need thirty words to post a ty note!!!

You know that u can use heart emoji right? :smirk: anyways enjoy your next 24hrs (have a nice day) bye!

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