Raycast no longer hits non-collide parts?

Hello, I’m currently scripting a room detection, and when I stand on a RoomHitbox, it prints correctly. However, as soon as I make it non-collide, it returns nil. Is there a property which disables non-collide parts?

raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = {workspace.RoomParts}
local raycastResult = workspace:Raycast(player.Character.HumanoidRootPart.Position, Vector3.new(0,-3,0), raycastParams)
print(raycastResult)

this is on a local script, and fires using a loop just for testing. It always prints nil when the part is non-collide(it’s still anchored).

1 Like

I thought that rays can hit non-collide parts but I might be incorrect. One thing you can do to make the part non-collide (but not really) is making a new collision group. Then just set the ray to only hit parts in the collision group with this property: RaycastParams.CollisionGroup. Then set the new collision group to not be able to collide with the default collision group.

If you are looking to not hit non-collide parts you can use recursion, which is where you recall a function inside that function. If you want more info about that just ask me.

1 Like

The weird thing is, that doesn’t even work! I’m also positive that rays hit non-collide, but right now even doing a separate CollisionGroup doesn’t work. I’m definitely doing something wrong?

local player = game.Players.LocalPlayer
local Part = script.Part:Clone()
local PhysicsService = game:GetService("PhysicsService")
PhysicsService:SetPartCollisionGroup(Part,"RoomParts")
Part.Parent = workspace 
while wait() do
	if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
		Part.Position = player.Character.HumanoidRootPart.Position + Vector3.new(0,100,0)
		local part = player.Character:FindFirstChild("HumanoidRootPart")
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
		raycastParams.FilterDescendantsInstances = {workspace.RoomParts}
		raycastParams.CollisionGroup = "RoomParts"
		local raycastResult = workspace:Raycast(part.Position, Vector3.new(0,-3,0), raycastParams)
		print(raycastResult)
		if raycastResult  then
		-- it is indoors my dudes
			for i,v in pairs(Part:GetChildren())do
				v:Clear()
				v.Enabled = false
			end
		else
		--it is outdoors my dudes

			for i,v in pairs(Part:GetChildren())do
				v.Enabled = true
			end
		end
	end
end

Edit:I was editting the wrong part’s collision group, let me try it again
Edit2:Still doesnt work, is my studio just bugged?

1 Like
--Local script
local player = game.Players.LocalPlayer
--I assume part is like a rain cloud or something
local Part = script.Part:Clone()
local PhysicsService = game:GetService("PhysicsService")
--Unless part is what you want your raycast to hit, change this to a for each in RoomParts
PhysicsService:SetPartCollisionGroup(Part,"RoomParts")
Part.Parent = workspace
while wait() do
	if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
		Part.Position = player.Character.HumanoidRootPart.Position + Vector3.new(0,100,0)
        --I recomend naming things useful names. It's a good habit.
		local part = player.Character:FindFirstChild("HumanoidRootPart")
		local raycastParams = RaycastParams.new()
        --[[
        If you set up the collision groups correctly there will not be a need for
        a filter. But because that wasn't the problem, I would leave this and
        not use the collision group stuff.
        ]]
        
		raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
		raycastParams.FilterDescendantsInstances = {workspace.RoomParts}
		raycastParams.CollisionGroup = "RoomParts"
        --[[
        The raycast's range is only three studs downwards from the player's
        hrp, which is 3 studs above the ground. That will probably cause a
        problem.
        ]]--
		local raycastResult = workspace:Raycast(part.Position, Vector3.new(0,-3,0), raycastParams)
		print(raycastResult)
		if raycastResult  then
		-- it is indoors my dudes
			for i,v in pairs(Part:GetChildren())do
				v:Clear()
				v.Enabled = false
			end
		else
		--it is outdoors my dudes

			for i,v in pairs(Part:GetChildren())do
				v.Enabled = true
			end
		end
	end
end

Possible Solutions

  • Increase range by 1 or 2
  • The ray starts inside the part you’re trying to hit, move the origin up a bit.
1 Like

Oh does it not work if it starts inside the part? the part covers the whole room, so that could be it.

Sorry to butt in here like this.

Butt, can you use just a normal Vector3 as the second parameter? I thought it had to be relative or something.
Like part.Position + Vector3.new(0,-3,0)

So:
workspace:Raycast(part.Position, part.Position + Vector3.new(0,-3,0), raycastParams)

I mean might as well worth a try, unless I’m wrong, or you can use it both ways.

I don’t think so, the second parameter is just the direction and how far it has to go.
like Vector3.new(0,5,0) will go up 5 studs, and part.CFrame.LookVector*500 will be 500 in the face of the part.