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
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
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)
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.
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
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)