Player Accessories Blocking shots from Gun System

Hi

  1. What do you want to achieve? i want to finish my gun system

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

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

no no no. Use character = raycast and raycast.Instance:FindFirstAncestorWhichIsA(“Model”)

local hum = character:FindFirstChildWhichIsA(“Humanoid”)

Note that I wrote this on mobile, so there are maybe some typos, but this should guide you to the right direction.

I guess you can do:

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

What do you mean professional? Don’t act like your current script is professional.

Not meant to be rude.

Why don’t you use my way of doing raycast bullet detection or how you call it. It works for me and always did

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

  1. 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
  2. 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

Raycast params do this for you :person_facepalming::person_facepalming::person_facepalming:

Okay, I will help you.

local fChar = Result:FindFirstAncestorWhichIsA(“Model”)

and then for the Humanoid we simply do

local hum = fChar:FindFirstChildWhichIsA(“Humanoid”)

–rest of code

Oh? My deepest apologies, I wasn’t aware of that but thank you for clarifying it

Turn the accessory handle’s “canquery” off. I can guarantee that it’ll fix problem.

There will be a better suggestion, but you could apply accessories to avoid colliding with the same collision group as the bullets.

wow, I wasnt really expecting that to work, searched why and understood immediately, thanks!

1 Like

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