You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
I want my script to insert all the Accessories that the player has into the Enum.RaycastFilterType.Exclude table, which works but it just doesn’t actually exclude them
What is the issue?
Here it has the exclude table printed, but it still doesn’t exclude the accessories (this was with 2 player test mode)
What solutions have you tried so far? I haven’t found anything that could help fix this anywhere
local player = game.Players.LocalPlayer
local tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local Dmg = 10
local deBounceTime = .25
local deBounce = false
tool.Activated:Connect(function()
if deBounce == false then
script.Sound:Play()
local mouse = player:GetMouse()
local char = tool.Parent
local accTable = {}
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
local filter = {char}
for _, v in char:GetChildren() do
if v:IsA("Accessory") then
table.insert(filter, v.Handle)
end
end
params.FilterDescendantsInstances = filter
print(params.FilterDescendantsInstances)
if mouse.Target == nil then
deBounce = true
wait(deBounceTime)
deBounce = false
else
local direction = mouse.Target.Position - tool.Handle.Position
local ray = workspace:Raycast(tool.Handle.Position, direction, params)
if ray then
local hit = ray.Instance
print(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if not hum and hit.Parent:IsA("Accessory") then
hum = hit.Parent.Parent:FindFirstChild("Humanoid")
end
if hum then
if hit == char.hitBoxHead then
Dmg = 100
RS.onShot:FireServer(Dmg,hum)
else
RS.onShot:FireServer(Dmg,hum)
end
end
end
deBounce = true
wait(deBounceTime)
deBounce = false
end
end
end)
.So i had this before, but i ran into the issue that i simply couldnt insert anything. so someone else told me to make a table, insert things in there, and then params.FilterDescendantsInstances = filter.
The issue isnt that it doesnt add things into the table, because it does, it just doesnt work like it needs to.
To resolve your issue, In my opinion, you should make these Raycast Params way before to increasing processing speed and problems like this.
You can either make your own “Filtering” system which checks the ClassName of an Instance or Ignore it like you did which wouldn’t be the best.
I’ve Refactored your code and changed it up a-bit. The main issue may be because of you putting the handle instead of the the accessory its self, Please reply back if your still having this issue.
-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
-- Variables
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local tool = script.Parent
local damage = 10
local debounceTime = 0.25
local debounce = false
-- Params
local raycastParams = RaycastParams.new()
-- Set up raycasting parameters
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local filter = {character}
-- Exclude accessory handles from raycasting (Just in case)
for _, accessory in ipairs(character:GetDescendants()) do
if accessory:IsA("Accessory") then
table.insert(filter, accessory)
end
end
raycastParams.FilterDescendantsInstances = filter
-- Event Connections
tool.Activated:Connect(function()
if debounce then
return
end
debounce = true
script.Sound:Play()
-- Get player mouse and tool's parent (assumed to be the character)
local mouse = player:GetMouse()
--local character = tool.Parent -- Character can be nil.
-- Ensure the player is targeting something
if mouse.Target then
local direction = mouse.Target.Position - tool.Handle.Position
local ray = Workspace:Raycast(tool.Handle.Position, direction, raycastParams)
if ray then
local hit = ray.Instance
local humanoid = hit.Parent:FindFirstChild("Humanoid")
-- If hit an accessory, check its parent for a humanoid
if not humanoid and hit:FindFirstAncestorOfClass("Accessory") then
humanoid = hit.Parent.Parent:FindFirstChild("Humanoid")
end
-- Handle damage application
if humanoid then
local finalDamage = damage
if hit == character:FindFirstChild("hitBoxHead") then
finalDamage = 100
end
ReplicatedStorage.onShot:FireServer(finalDamage, humanoid)
end
end
end
-- Debounce cooldown
task.wait(debounceTime)
debounce = false
end)
This post is also quite repetitive as I’ve seen other Posts’ resolving this issue.
I’ve looked into this, I cant quite tell what stuff u have changed to my script. I’m not just trying to copy everything and hope it works, I’m also trying to learn from this, and see WHAT is wrong exactly.
I’ve tested your for loop, it doesn’t change anything. Also you say the reason is because I was excluding the handle, which isn’t quite true. The Raycast prints out “Handle”, clicking on it gives me the handle of the accessory which I clicked on.
Nothing really changed that much, are u sure this is the way? It still just tells me I hit the handle, while the handle/accessory is filtered out.
I don’t see anything inherently wrong with your code. How do you know accessories aren’t being excluded? On another note, here is a test program I whipped up that appears to work on my end:
--!strict
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RAY_LENGTH = 1_000
local Camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local raycastParameters
local function onCharacterAdded(character: Model & any)
local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
raycastParameters = RaycastParams.new()
raycastParameters.FilterDescendantsInstances = humanoid:GetAccessories()
raycastParameters.FilterType = Enum.RaycastFilterType.Exclude
end
local function onInputBegan(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent or not raycastParameters then
return
end
if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
return
end
local mouseLocation = UserInputService:GetMouseLocation()
local ray = Camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * RAY_LENGTH, raycastParameters)
if not result then
return
end
print(result.Instance:GetFullName())
end
local character = Player.Character
if character then
onCharacterAdded(character)
end
Player.CharacterAdded:Connect(onCharacterAdded)
UserInputService.InputBegan:Connect(onInputBegan)
Edit: I made a new place, without anything in it, and I put the LocalScript in the StarterPlayerScripts as well. I put all of your code into it and it didn’t work as well.
Idk why he’s complicating stuff, but this seems to work now as its directly fetching the accessories.
-- Services
local UserInputService = game:GetService("UserInputService")
local PlayerService = game:GetService("Players")
-- Objects
local Camera = workspace.CurrentCamera
local Player = PlayerService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
-- Values
local raycastParameters = RaycastParams.new()
-- Constants
local RAY_LENGTH = 1000
-- Functions
function getAccessories() : {any?}
local accessories = {}
for _, object in workspace:GetDescendants() do
if object:IsA("Accessory") and not object:IsAncestorOf(Character) then
table.insert(accessories, object)
continue
else
continue
end
end
return accessories
end
function combineTables(RootTable, Table) : {any?}
local newTable = table.clone(RootTable)
for _, value in Table do
table.insert(newTable, value)
end
return newTable
end
function Setup() -- just refactoring..
raycastParameters.FilterDescendantsInstances = {Character}
task.spawn(function()
local accessories = getAccessories()
raycastParameters.FilterDescendantsInstances = combineTables(
raycastParameters.FilterDescendantsInstances,
accessories
)
end)
end
function onInputBegan(input: InputObject, typing: boolean)
if typing or not raycastParameters then
return
end
if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
return
end
local mouseLocation = UserInputService:GetMouseLocation()
local cameraRay = Camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local rayCast = workspace:Raycast(cameraRay.Origin, cameraRay.Direction * RAY_LENGTH, raycastParameters)
if not rayCast then
return
end
local instance = rayCast.Instance
print(instance.Name, instance.Parent)
end
function charactedAdded(model)
if model == Character then
return
end
raycastParameters.FilterDescendantsInstances = {Character}
task.spawn(function()
local accessories = getAccessories()
raycastParameters.FilterDescendantsInstances = combineTables(
raycastParameters.FilterDescendantsInstances,
accessories
)
end)
end
-- Connections
Setup()
UserInputService.InputBegan:Connect(onInputBegan)
Player.CharacterAdded:Connect(charactedAdded)
I tested this and it worked, I also figured out how it worked, but not WHY.
What I did is get the descendants of the character, which wouldn’t want to work. What did work is get ALL the descendants of workspace, and check if any are accessories, I changed my own script to this and now it works flawlessly. Thanks!