hello, i have a raycast script and i need help adding a ignore list, i need to ignore everything in a folder since its getting in the way of the raycast, any help?
local player = game.Players.LocalPlayer
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local NeedtoBeIgnored = game.Workspace.PassThroughs -- Folder that has the parts that need to be ignored
RunService.RenderStepped:Connect(function(step)
local ray = Ray.new(hrp.Position, ((hrp.CFrame + hrp.CFrame.LookVector * 2) - hrp.Position).Position.Unit * 1000)
local ignoreList = char:GetChildren()
local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
if hit then
print(hit)
if char:WaitForChild("Camera") then
if char:WaitForChild("Camera").Handle.SpotLight.Enabled == true then
if hit.Parent.Name == "Monster" then
game.ReplicatedStorage.Blinded:FireServer()
end
end
end
end
end)
You should use workspace:Raycast() instead of Ray.new(), here is an example:
local part = workspace.part
local ignorelist = {workspace.otherpart1,workspace.otherpart2}
local origin = part.Position
local direction = part.CFrame.LookVector
-- You don't need to add the position to the direction as the function already does that.
local params = RaycastParams.new()
params.FilterDescendantsInstances = ignorelist
-- ignorelist HAS to be a table or else this will not work.
local result = workspace:Raycast(origin,direction,params)
I think this should work, I replaced Ray.new() with workspace:Raycast()
local player = game.Players.LocalPlayer
local char = player.Character
local hrp:BasePart = char:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local NeedtoBeIgnored = game.Workspace.PassThroughs -- Folder that has the parts that need to be ignored
RunService.RenderStepped:Connect(function(step)
local origin = hrp.Position
local direction = hrp.CFrame.LookVector * 1000
local params = RaycastParams.new()
params.FilterDescendantsInstances = NeedtoBeIgnored:GetChildren()
local result = workspace:Raycast(origin,direction,params)
-- Properties of result :
-- result.Instance = The part it hit
-- result.Position = The position on where it hit
-- result.Distance = The distance between the start and the end
-- result.Material = The material of the part it hit
-- result.Normal = The rotation of the surface it hit
if result then
print(result.Instance,result.Position)
if char:FindFirstChild("Camera") then -- Don't use :WaitForChild() in if statements. This will cause an infinite yield.
if char:FindFirstChild("Camera").Handle.SpotLight.Enabled == true then
if result.Instance.Parent.Name == "Monster" then
game.ReplicatedStorage.Blinded:FireServer()
end
end
end
end
end)