What do you want to achieve? i want to finish my gun system
What is the issue? so, accessories(handles in general) are blocking the gunshots from my sniper gun, im 100% sure it has to do with the RaycastParams exclude table that ill share down here, but i dont know how to exclude handles and anything besides the Player character
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i did stumble across a devforum post of someone with a similar issue, they fixed it by excluding all the descendants of the player character, i tried that and didnt work, i tried like other 2 methods that were quite similar and they didnt work, i dont know what to do anymore.
ok heres the code, it handles the shooting and the damage handling and all that
local Sniper = script.Parent
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ShootEvent = Sniper.ShootSignal
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local GunRange = 2500
function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
ShootEvent.OnServerEvent:Connect(function(Player, MousePosition, Target)
local ShootSound = Sniper.SniperShootSound:Clone()
ShootSound.Parent = Sniper.Handle
ShootSound:Play()
if Sniper == nil then return end
local Damage = Sniper.Damage.Value
local Direction = (MousePosition - Sniper.Handle.Position) * GunRange
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Exclude
local Result = workspace:Raycast(Sniper.Handle.Position, Direction, RayParams)
local Bullet = RS.Weapons.Sniper.Bullet:Clone()
Bullet.CFrame = Sniper.Handle.BulletExit.CFrame
Bullet.Parent = workspace
local BulletDuration = 0.2 * ((Direction/GunRange).Magnitude)/50
TweenService:Create(Bullet, TweenInfo.new(BulletDuration), {CFrame = CFrame.new(MousePosition)}):Play()
if not Target then
task.delay(BulletDuration, function()
Bullet:Destroy()
end)
else
task.delay(1, function()
Bullet:Destroy()
end)
end
if Result == nil then return end
local Hum = Result.Instance.Parent:FindFirstChild("Humanoid")
if Hum == nil then return end
task.wait(BulletDuration)
UntagHumanoid(Hum)
TagHumanoid(Hum, Player)
Hum:TakeDamage(Damage)
end)
the following lines are the ones im having trouble with :
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Exclude
local Hum
if Result.Instance.Parent:IsA(“Accessory”) then
Hum = Result.Instance.Parent.Parent:FindFirstChild(“Humanoid”)
else
Hum = Result.Instance.Parent:FindFirstChild(“Humanoid”)
end
But I dont think It’s a professional way to solve it, anyway It might work
Sorry, I dont mean to act like something. Theres alot things I need to know. I just use FindFirstChild everytime so thats why I dont understand your way of doing raycast first time. Thanks anyway now I got it
You can try to make the raycast ignore all the parts of the player character (This includes handles) and any other unnecessary accessories
You can exclude all descendants of the character except essential parts, so instead of excluding only the player’s character, we can refine the filter to exclude certain specific parts for example (Handle) from the raycast
Instead of directly excluding the entire Character model, you can exclude specific parts that you don’t want to block the ray for example, Handles, Accessories and so on
This script should help you with that
local Sniper = script.Parent
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ShootEvent = Sniper.ShootSignal
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local GunRange = 2500
-- Function to tag the humanoid for kill credit
function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
-- Function to untag the humanoid
function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
-- Helper function to get all descendants to exclude from raycast
local function GetAllDescendantsToExclude(character)
local partsToExclude = {}
for _, descendant in pairs(character:GetDescendants()) do
if descendant:IsA("BasePart") then
table.insert(partsToExclude, descendant)
end
end
return partsToExclude
end
ShootEvent.OnServerEvent:Connect(function(Player, MousePosition, Target)
local ShootSound = Sniper.SniperShootSound:Clone()
ShootSound.Parent = Sniper.Handle
ShootSound:Play()
if Sniper == nil then return end
local Damage = Sniper.Damage.Value
local Direction = (MousePosition - Sniper.Handle.Position) * GunRange
-- Create RaycastParams
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = GetAllDescendantsToExclude(Player.Character) -- Exclude all character parts
RayParams.FilterType = Enum.RaycastFilterType.Exclude
-- Perform the raycast
local Result = workspace:Raycast(Sniper.Handle.Position, Direction, RayParams)
local Bullet = RS.Weapons.Sniper.Bullet:Clone()
Bullet.CFrame = Sniper.Handle.BulletExit.CFrame
Bullet.Parent = workspace
local BulletDuration = 0.2 * ((Direction / GunRange).Magnitude) / 50
-- Tween the bullet to the target position
TweenService:Create(Bullet, TweenInfo.new(BulletDuration), {CFrame = CFrame.new(MousePosition)}):Play()
if not Target then
task.delay(BulletDuration, function()
Bullet:Destroy()
end)
else
task.delay(1, function()
Bullet:Destroy()
end)
end
-- If the raycast hit nothing, exit early
if Result == nil then return end
-- Check if the raycast hit a humanoid
local Hum = Result.Instance.Parent:FindFirstChild("Humanoid")
if Hum == nil then return end
-- Apply damage after the bullet reaches the target
task.wait(BulletDuration)
UntagHumanoid(Hum)
TagHumanoid(Hum, Player)
Hum:TakeDamage(Damage)
end)
This will ensure that the raycast ignores all parts of the player’s character by excluding them using the GetAllDescendantsToExclude function, this will prevent the raycast from being blocekd by accessories or character parts and will esnure that only actual targets are detected