Ignoring accessories for ray bullet detection

Hi!
Currently i’m working on fixing my hit detection for my weapons.
Right now, the guns don’t ignore the accessories of the player.

Here is the code i’m using to get the accessories.

local rayIgnoreList = {}


player.CharacterAppearanceLoaded:Connect(function(playerChar)
	local humanoid = playerChar:WaitForChild("Humanoid")
	if humanoid then
		print("Humanoid Detected")
		for i, v in pairs(humanoid:GetAccessories()) do
			if v:IsA("Accessory")then
				table.insert(rayIgnoreList, v)
				print("Inserted: ", v)
			end
		end
	end
end)

	local function GetObject(Array) --- a quick function

			for _, Val in pairs(Array) do -- for each index in the array given, do
				return Val -- return the object/value
			end
		end

		local object = GetObject(rayIgnoreList)
		
		if object then
			object.Handle.CanQuery = false
			object.Handle.CanTouch = false
			object.Handle.CanCollide = false
		end

My issue is that it just plain doesnt work at all.

here’s my code for casting the bullets:

local bullet = game:GetService("ReplicatedStorage").Tracers.PistolTracer:Clone()
	bullet.Name = "BulletTracer"
	bullet.Size = Vector3.new(0.1, 0.1, 0.1)
	bullet.Anchored = true
	bullet.CanCollide = false
	bullet.Parent = game.Workspace
	bullet.CFrame = CFrame.new(origin, endposition)  * CFrame.Angles(math.rad(math.random(-spread,spread)), math.rad(math.random(-spread,spread)), 0)
	bullet.CollisionGroup = "Bullet"
	local bulletHoleType = false
	local Loop
	
	
	--< HIT LOGIC INSIDE >--
	Loop = game:GetService("RunService").Heartbeat:Connect(function(dt)
		
		local function GetObject(Array) --- a quick function

			for _, Val in pairs(Array) do -- for each index in the array given, do
				return Val -- return the object/value
			end
		end

		local object = GetObject(rayIgnoreList)
		
		if object then
			object.Handle.CanQuery = false
			object.Handle.CanTouch = false
			object.Handle.CanCollide = false
		end
		
		local TestRay = Ray.new(bullet.Position, bullet.CFrame.LookVector * velocity)
		local PartHit, WorldPosition, WorldNormal = workspace:FindPartOnRayWithIgnoreList(TestRay, {game.Players.LocalPlayer.Character, script.Parent, object, object:FindFirstChild("Handle")})
		bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
		
		--< HIT LOGIC >--
		if ( PartHit ) then
			if ( PartHit ) then
				createBulletHole(PartHit, WorldPosition)
			end 
			if ( PartHit ).Parent:FindFirstChild("HumanoidRootPart") then
				if ( PartHit ) == ( PartHit ).Parent:FindFirstChild("HumanoidRootPart") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Torso") and damage ~= nil then
					damage:FireServer(( PartHit ).Parent, (PartHit), 34)
					local clone = player.PlayerGui.Template.Crosshair.hitmarker:Clone()
					clone.Parent = game.Workspace
					clone.Parent = nil
					clone:Destroy()
					bullet:Destroy()
					Loop:Disconnect()
					h.ImageTransparency = 0
					tween:Create (h, TweenInfo.new(0.25), {ImageTransparency = 1}):Play()
				else
					bullet:Destroy()
					Loop:Disconnect()
				end
				if ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Head") and damage ~= nil or ((PartHit.Parent:IsA("Accessory")) or PartHit:IsA("Accessory") and damage ~= nil) or ((PartHit).Parent == object and damage ~= nil) then
					if (PartHit).Parent == object and damage ~= nil then
						damage:FireServer(( PartHit ).Parent.Parent, (PartHit), 42)
					end
					local clone = player.PlayerGui.Template.Crosshair.hitmarker:Clone()
					clone.Parent = game.Workspace
					clone.Parent = nil
					clone:Destroy()
					bullet:Destroy()
					Loop:Disconnect()
					hs.ImageTransparency = 0
					tween:Create (hs, TweenInfo.new(0.25), {ImageTransparency = 1}):Play()
				else
					bullet:Destroy()
					Loop:Disconnect()
				end
				if ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Left Arm") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Right Arm") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Right Leg") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Left Leg") and damage ~= nil then
					damage:FireServer(( PartHit ).Parent, (PartHit), 25)
					local clone = player.PlayerGui.Template.Crosshair.hitmarker:Clone()
					clone.Parent = game.Workspace
					clone.Parent = nil
					clone:Destroy()
					bullet:Destroy()
					Loop:Disconnect()
					h.ImageTransparency = 0
					tween:Create (h, TweenInfo.new(0.25), {ImageTransparency = 1}):Play()
				else
					bullet:Destroy()
					Loop:Disconnect()
				end
			end
		elseif bullet.Position.magnitude > 1000 and ( PartHit ) == nil then
			bullet:Destroy()
			Loop:Disconnect()
		end
	end)

Can anybody tell me what’s wrong with this? i’ve been struggling for weeks.

Thanks!

4 Likes

You can set all of the accessory parts CanQuery property to false

4 Likes

is it because i did it in a local script?

4 Likes

Yeah you need to set it all to false on the server because other clients won’t see it as false when they cast the ray.

5 Likes

okay, so.

i did it on the server, but i havent made it so that it detects which kind of accessory it is to do the respective damage.

where would i implement it in the damage detection code? how could i check if for example its a face accessory, waist, front, back etc.

edit: it still querys the accessories. is it because its Ray.new?

4 Likes

I thought you just wanted to ignore accessory detection? Why do you want different damages for each accessory? If you wanted the damage to be different based off the accessory it hit then wouldn’t you want to hit the accessories instead of ignoring them?

3 Likes

well, it is hitting them, which is my problem. as i just asked, is it because im not using a raycast?

3 Likes

Your issue is that you’re turning the CanQuery to false after you’ve casted the ray. Right when the character spawns set all of their accessory CanQuery’s to false

1 Like

i just went into the server script and did this:

game.Players.PlayerAdded:Connect(function(plr)			
	plr.CharacterAppearanceLoaded:Connect(function(playerChar)
		local humanoid = playerChar:WaitForChild("Humanoid")
		if humanoid then
			print("Humanoid Detected")
			for i, v in pairs(humanoid:GetAccessories()) do
				if v:IsA("Accessory")then
					table.insert(rayIgnoreList, v)
					print("Inserted: ", v)
				end
			end
		end
	end)
	
	local function GetObject(Array) --- a quick function

		for _, Val in pairs(Array) do -- for each index in the array given, do
			return Val -- return the object/value
		end
	end

	local object = GetObject(rayIgnoreList)

	if object then
		object.Handle.CanQuery = false
		object.Handle.CanTouch = false
		object.Handle.CanCollide = false
	end
2 Likes

When you go on test mode select your accessory and see if it’s CanQuery is false

2 Likes

thank you for the help, i wasnt doing it right

1 Like

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