How do I ignore other bullets in my raycast gun?

Here is what it looks like: https://gyazo.com/7c2604e9790962deafab38af795bb512

See how there’s just a group of bullets when I aim directly at the muzzle of the gun? It kinda looks like a dot. That’s what I wanna get rid of.

I was thinking of using an ignore list, but would that really work? I need your guys’ help and opinions. Thank you.

SCRIPT:

Server script:

local remote = game.ReplicatedStorage:WaitForChild(“Remotes”):WaitForChild(“GunEvent”)
local cam = game.Workspace.CurrentCamera

remote.OnServerEvent:Connect(function(player, barrelPos, mousePos)
local ray = Ray.new(barrelPos, (mousePos - barrelPos).unit * 50)
local part, position = game.Workspace:FindPartOnRay(ray, player.Character, false, true)

local dist = math.clamp((barrelPos - mousePos).magnitude, 0.05, 50)

if part then
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid:TakeDamage(7)
	end
end

remote:FireAllClients(player, part, position, dist, barrelPos)

end)

Local script:

local remote = game.ReplicatedStorage:WaitForChild(“Remotes”):WaitForChild(“GunEvent”)
local TweenService = game:GetService(“TweenService”)

remote.OnClientEvent:Connect(function(player, part, position, dist, barrelPos)

local bullet = Instance.new("Part")
bullet.Anchored = true
bullet.CanCollide = false
bullet.BrickColor = BrickColor.new("White")
bullet.Name = "bullet"

bullet.Size = Vector3.new(0.075,0.075,dist)
bullet.Transparency = 0.95
bullet.Material = Enum.Material.Neon
bullet.CFrame = CFrame.new(barrelPos, position) * CFrame.new(0,0, -dist/2)

bullet.Parent = game.Workspace

game.Debris:AddItem(bullet, 5)

local Info = TweenInfo.new(

1,


Enum.EasingStyle.Cubic, 

Enum.EasingDirection.Out,

0, 
false,

0 

)

local Goals =

{

Transparency = 1;

}

local tween = TweenService:Create(bullet,Info,Goals)

tween:Play()

end)

I changed the transparency tween longer to make it easier for you guys to see it.

Did you test an ignore list? I would do that before anything else.

It’s kinda hard to see your code too. Most of it is unformatted

Yes, I made an ignore list, but I don’t know how I should put the bullets inside the list.

coroutine.resume(coroutine.create(function()
local IgnoreList = {player.Character,
workspace.Camera,
cam
}

	for i,v in pairs(workspace:GetDescendants()) do
		if (v:IsA("Accessory"))
		or (v:IsA("Tool"))
		or (v:IsA("Part")) and v.Transparency >= 1  then
			table.insert(IgnoreList,v)
		end
	end

I was maybe thinking of putting the bullets that are fired into a folder into the workspace and then put that folder into the ignore list, would that probably work?

That would probably work. Just out of curiosity, why are you putting the camera in your ignore list? I don’t think you have to

Oh it’s because sometimes when I shoot, the bullet sometimes does this weird thing where the camera interferes with the bullet and it causes the bullet to go the wrong way. I don’t know how to really explain it, but after adding the camera to the ignore list, that issue is no longer a problem.

Try storing the bullets inside a folder, and use that folder as the ignore list.

Yes I just tried that, now the bullets aren’t even showing. Here’s my script:

local remote = game.ReplicatedStorage:WaitForChild(“Remotes”):WaitForChild(“GunEvent”)
local cam = game.Workspace.CurrentCamera
local bulletsFolder = game.Workspace.bulletsFolder:GetDescendants()

remote.OnServerEvent:Connect(function(player, barrelPos, mousePos)
local ray = Ray.new(barrelPos, (mousePos - barrelPos).unit * 50)
local part, position = game.Workspace:FindPartOnRay(ray, player.Character, false, true)

coroutine.resume(coroutine.create(function()
	local IgnoreList = {player.Character,
	workspace.Camera,
	cam,
	bulletsFolder
		}
		
	for i,v in pairs(workspace:GetDescendants()) do
		if (v:IsA("Accessory"))
		or (v:IsA("Tool"))
		or (v:IsA("Part")) and v.Transparency >= 1  then
			table.insert(IgnoreList,v)
		end
	end

local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)

local dist = math.clamp((barrelPos - mousePos).magnitude, 0.05, 50)

if part then
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid:TakeDamage(7)
	end
end

remote:FireAllClients(player, part, position, dist, barrelPos)

end))
end)

I already made it so that the bullet’s parent is the folder inside workspace in my other script.

Wait, why are you using :GetDescendants() on the bulletsFolder?

It should be:
local bulletsFolder = game.Workspace.bulletsFolder

Also, why are you trying to :FindPartOnRay() twice? Take this one out because you already have a line that has :FindPartOnRayWithIgnoreList()
local part, position = game.Workspace:FindPartOnRay(ray, player.Character, false, true)

Is this a local or a server script? It’s kinda confusing to look at.

Sorry, this is a server script. I’m really new to Lua so I’m not that really organized Lol

Does this look correct now?

image

It’s all good bro, I’m just kinda bamboozled.

This is what I have so far (Server Side):

local remote = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("GunEvent")
local cam = game.Workspace.CurrentCamera
local bulletsFolder = game.Workspace.bulletsFolder
local ignore = {}

remote.OnServerEvent:Connect(function(player, barrelPos, mousePos)
	local ray = Ray.new(barrelPos, (mousePos - barrelPos).Unit * 50)
	
	for _,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA('Accessory') or v:IsA('Tool') or v:IsA('Part') and v.Transparency >= 1 then
			table.insert(ignore, v)
		end
	end
	
	local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)
	local dist = math.clamp((barrelPos - mousePos).Magnitude, 0.05, 50)

	if part then
		local humanoid = part.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid:TakeDamage(7)
		end
	end
end)

Both .unit and .magnitude are deprecated (can’t be used for new work). Use .Unit and .Magnitude instead.

It seems as if you’re doing all of this under one event. Do you want the bullets to be rendered client-side or server-side?

The bullets are rendered through the client side, I did a tweening effect on the client side and it works perfectly on the server side.

https://gyazo.com/2470831cae875d870f9a419965c1d61b?token=cf5d207e4cf01c587c034316c9e40465

Still doesn’t work for some reason. I capitalized the U and M in unit and magnitude and it still does the same thing as before.

I forgot about your client-side code :joy:. Try doing this and see if it works, 'cause you didn’t parent the bullet inside the bulletsFolder.

local remote = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("GunEvent")
local TweenService = game:GetService("TweenService")
local bulletsFolder = game.Workspace.bulletsFolder

remote.OnClientEvent:Connect(function(player, part, position, dist, barrelPos)

	local bullet = Instance.new("Part")
	bullet.Anchored = true
	bullet.CanCollide = false
	bullet.BrickColor = BrickColor.new("White")
	bullet.Name = "bullet"
	
	bullet.Size = Vector3.new(0.075,0.075,dist)
	bullet.Transparency = 0.95
	bullet.Material = Enum.Material.Neon
	bullet.CFrame = CFrame.new(barrelPos, position) * CFrame.new(0,0, -dist/2)
	
	bullet.Parent = bulletsFolder

	game.Debris:AddItem(bullet, 5)
	local Info = TweenInfo.new(1,Enum.EasingStyle.Cubic, Enum.EasingDirection.Out,0,false,0)
	
	local Goals = {Transparency = 1}
	
	local tween = TweenService:Create(bullet,Info,Goals)
	
	tween:Play()
end)

Yeah, I already parented the bullets to the folder. In this GIF you can still see how you can shoot bullets onto other bullets because of the collision.

GIF:

https://gyazo.com/be368875e65200cd19c0fc8f3b152c88

For the server-script, try doing this:

local remote = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("GunEvent")
local cam = game.Workspace.CurrentCamera

remote.OnServerEvent:Connect(function(player, barrelPos, mousePos)
	local ray = Ray.new(barrelPos, (mousePos - barrelPos).Unit * 50)
	local ignore = {game.Workspace.bulletsFolder, player.Character}
	for _,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA('Accessory') or v:IsA('Tool') or v:IsA('Part') and v.Transparency >= 1 then
			table.insert(ignore, v)
		end
	end
	
	local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)
	local dist = math.clamp((barrelPos - mousePos).Magnitude, 0.05, 50)

	if part then
		local humanoid = part.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid:TakeDamage(7)
		end
	end
end)

And for this line right here, were you using this for debugging purposes?

for _,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA('Accessory') or v:IsA('Tool') or v:IsA('Part') and v.Transparency >= 1 then
			table.insert(ignore, v)
		end
	end

you should not be able to insert objects in tables

you should do

for _, v in pairs(workspace:GetDescendants()) do
     table.insert(yourtablehere, v.Name)
end

if this does not solve the issue then is there any error in the output

I think I know what’s the problem. It’s not the bullets, but rather than the invisible barrel on the gun. Whenever I aim at that barrel, that’s where the bullets start forming a dot. So how do I get all children of my gun tool and make it part of the ignore list?