RaycastParams.FilterType not working

  1. What do you want to achieve? I have a script that fires a raycast from the player’s mouse and on collision deletes the instance. but excludes the local player from the collision of the raycast

  2. What is the issue? Since the game is locked in first person sometimes the player can hit their torso when looking down which will kill the player, i tried to excluding the local player from the raycast but that didnt work so after many attempts im making this post

  3. What solutions have you tried so far? i looked on the devforum, youtube and other sites but nothing has worked yet

--this isnt the entire localscript (that i put in StarterPlayerScripts)
--the only thing that doesnt work is the RaycastParams.FilterType stuff
local RaycastParams = RaycastParams.new()

RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
RaycastParams.FilterType = game.Players.LocalPlayer




local function CreateRayCast()
	local mousePosition = uis:GetMouseLocation()
	local rayCast = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
	local rcResult = workspace:Raycast(rayCast.Origin,rayCast.Direction * 200)
	return rcResult
end

uis.InputBegan:Connect(function(input,busy)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not busy then
		if item:IsA("BasePart") then
			item:Destroy()
		end
	end
end)

You can’t filter a player instance, also you wouldn’t set the filter type to Exclude then to Localplayer. It would be like this

RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
RaycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
1 Like

didnt know that i cant filter the player lol. the game still lets me shoot the player for some reason so im gonna try out something but thanks you :grin:

It still lets you shoot the player’s character because you never included the RaycastParams in the first place, meaning it isn’t considered when raycasting. Just update the code line above to this:

local rcResult = workspace:Raycast(rayCast.Origin,rayCast.Direction * 200, RaycastParams)

1 - FilterType will only decide should the raycast ignores or includes the object that was hit and it not gonna filtering anything out when you set it to an object.

2 - FilterDescendantsInstances is built to store a list of objects that the raycast should ignore or include those objects when the ray hits.

3 - workspace:Raycast() will take 3 arguments. Origin, Direction, and the RaycastParams. So if you pass in the RaycastParams then it will apply the configurations that u have made on the RaycastParams.

Raycast Tutorial (If you need to learn how to use raycast): https://www.youtube.com/watch?v=4fJBWFz9I-E

New script (paste this in):

local RaycastParams = RaycastParams.new()
RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
RaycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}

local function CreateRayCast()
	local mousePosition = uis:GetMouseLocation()
	local rayCast = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
	local rcResult = workspace:Raycast(rayCast.Origin,rayCast.Direction * 200, RaycastParams)
	return rcResult
end

uis.InputBegan:Connect(function(input,busy)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not busy then
		if item:IsA("BasePart") then
			item:Destroy()
		end
	end
end)

didnt work for some reason but after i rewrote the entire part now it works so thanks to everyone who was helping me :smiley:

heres the entire code if someone wants it:

--raycasttttttttttttttt
--uis = UserInputService



local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local camera = workspace.Camera
local item = nil
local sg = game:GetService("StarterGui")




--idk
local function CreateRayCast()
	local mousePosition = uis:GetMouseLocation()
	local rayCast = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)

	local rayOrigin = rayCast.Origin
	local rayDirection = rayCast.Direction * 200

	local RaycastParams = RaycastParams.new()
	RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}

	local rcResult = workspace:Raycast(rayOrigin, rayDirection, RaycastParams)
	return rcResult
end

uis.InputBegan:Connect(function(input, busy)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not busy then
		local rcResult = CreateRayCast()
		if rcResult then
			local hitPart = rcResult.Instance
			if hitPart and hitPart:IsA("BasePart") then
				hitPart:Destroy()
			end
		end
	end
end)



--rayCast.Direction * kolko studov to ma ist do dialky



rs.RenderStepped:Connect(function()
	local result = CreateRayCast()
	if result and result.Instance then
		item = result.Instance
		print(item.Name)
	end
	
end)

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