Raycast blocking not working

I’m trying to make a gun using raycasts. If the object the raycast is hitting is a descendant of the Map folder, then the ray shouldn’t go through it. The problem with it is that it doesn’t work. Anyone able to help?

Code:

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")

		local descendants = game.Workspace.Map:GetDescendants()

		for index, descendant in pairs(descendants) do
			if hitPart == descendant then
				print("Object in the way!")
			else
				if model then
					if model:FindFirstChild("Humanoid") then
						model.Humanoid:TakeDamage(7)
					end
				end
			end
		end
	end
end)

Ok, instead of trying to see if it’s a descendant trought a loop, you can just use :IsDescendantOf(), it is a function that tells if the object is descendant of the variable inside it, if it is, it returns true, if its not, it returns false.

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		if hitPart:IsDescendantOf(workspace.Map) then -- this function verifies if it is a descendant of Map for you
			print("Object in the way!")
		else
			local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
			if humanoid then -- if it has a humanoid
				humanoid:TakeDamage(7)
			end
		end
	end
end)
2 Likes

Thanks! But I still face this problem:
If a player is behind a wall for example, they will still be damaged.
Would there be a workaround for this?

Actually that shouldn’t happen, as you are only ignoring the character, unless the wall was inside of it.
Maybe you can try to use this raycast instead to see if something changes:

local hitPart = workspace:FindPartOnRay(Ray.new(script.Parent.Handle.Muzzle.Position,mousePos - script.Parent.Handle.Muzzle.Position),player.Character)

Hm, still shoots through walls. Nothing changed.

Idk what the problem could be, the wall has cancollide on and cantouch on so I don’t really know.

Still unable to find a solution. lemme know if you find a solution!

Could it be possible that you set the Mouse.TargetFilter property to the Map Folder?

I don’t even know how to change it lol

Try implementing another check?

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		if hitPart:IsDescendantOf(workspace.Map) then -- this function verifies if it is a descendant of Map for you
			print("Object in the way!")
		else
			local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
			if humanoid then -- if it has a humanoid
				humanoid:TakeDamage(7)
			end
		end
    else
        print("Nothing hit apparently?")
	end
end)
1 Like

Still nothing, still goes through walls.

Is the wall apart of the Map folder? I know it’s dumb but just to make sure.

yeah it is, checked that already

The annoying part is that it’s really complicated to find an answer. Roblox should just add something like an enum for collisions with raycasts.

What

We have FilterType properties though, but for some reason yours is not working

Are you checking your Output at all btw?

Yeah, nothing in the output. Nothing being sent.