Bullets shoot from both guns?

Was making a simple gun and not sure what really went wrong here, the bullets shoot from both guns, even if the gun is unequipped

Code

Client

local Tool = script.Parent
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Event = game.ReplicatedStorage:WaitForChild("MainEvent")
local Bullet = workspace.Ignored.ClientBullet
local Debris = game:GetService("Debris")
local IsUsing = false

-- For Easier Changing --
local BulletSize = 0.2
local BulletColor = Color3.new(1,1,0)
local RayLength = 300
-------------------------

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {}
Params.FilterType = Enum.RaycastFilterType.Blacklist


local function OnEquipped()
	local C = {}
	local Holder = Tool.Parent
	table.insert(C, Holder)
	for i, v in ipairs(workspace.Ignored:GetChildren()) do
		table.insert(C, v)
	end
	Params.FilterDescendantsInstances = C
end

local function BulletGen(ray)
	local Rv = ray.Direction * RayLength
	local Raycast = workspace:Raycast(ray.Origin, Rv, Params)

	local HitPos = if Raycast then Raycast.Position else ray.Origin + Rv

	local ShootFrom = Tool.Handle.Attachment.WorldPosition

	local AimAt = HitPos - ShootFrom
	local HitDetection = workspace:Raycast(ShootFrom, AimAt * 2, Params)

	local ThingHit = if HitDetection then HitDetection.Instance else nil

	local PosHit = if HitDetection then HitDetection.Position else HitPos
	
	local D = Bullet:Clone()
	D.Color = BulletColor
	D.Size = Vector3.new(BulletSize, BulletSize, (ShootFrom - PosHit).Magnitude)
	D.CFrame = CFrame.lookAt(ShootFrom:Lerp(PosHit, .5), PosHit)
	D.Parent = workspace.Ignored
	Debris:AddItem(D, .05)
end

local function ClientBullet(From, To)
	local ReplicatedBullet = Bullet:Clone()
	ReplicatedBullet.Color = BulletColor
	ReplicatedBullet.Size = Vector3.new(BulletSize, BulletSize, (From - To).Magnitude)
	ReplicatedBullet.CFrame = CFrame.lookAt(From:Lerp(To, .5), To)
	ReplicatedBullet.Parent = workspace.Ignored
	Debris:AddItem(ReplicatedBullet, .05)
end

Tool.Activated:Connect(function()
	IsUsing = true
end)

Tool.Deactivated:Connect(function()
	IsUsing = false
end)

game:GetService("RunService").Stepped:Connect(function()
	if IsUsing == true then	
		local mouse = UIS:GetMouseLocation()
		local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
		Event:FireServer(ray)
		BulletGen(ray)
	end
end)


script.Parent.Equipped:Connect(OnEquipped)
Event.OnClientEvent:Connect(ClientBullet)

Server

local Tool = script.Parent
local Event = game.ReplicatedStorage.MainEvent
local Params = RaycastParams.new()
local debounce = false
Params.FilterDescendantsInstances = {}
Params.FilterType = Enum.RaycastFilterType.Blacklist

local function OnEquipped()
	local C = {}
	local Holder = Tool.Parent
	table.insert(C, Holder)
	for i, v in ipairs(workspace.Ignored:GetChildren()) do
		table.insert(C, v)
	end
	Params.FilterDescendantsInstances = C
end

local function Shoot(Player, ray)
	local Rv = ray.Direction * 300
	local Raycast = workspace:Raycast(ray.Origin, Rv, Params)
	
	local HitPos = if Raycast then Raycast.Position else ray.Origin + Rv
	
	local ShootFrom = Tool.Handle.Attachment.WorldPosition
	
	local AimAt = HitPos - ShootFrom
	local HitDetection = workspace:Raycast(ShootFrom, AimAt * 2, Params)
	
	local ThingHit = if HitDetection then HitDetection.Instance else nil
	
	local PosHit = if HitDetection then HitDetection.Position else HitPos
	
	if ThingHit then
		print(ThingHit)
	end
	
	for i, v in ipairs(game.Players:GetPlayers()) do
		if v ~= Player and v then
			Event:FireClient(v, ShootFrom, PosHit)
		end
	end
end



Event.OnServerEvent:Connect(Shoot)
Tool.Equipped:Connect(OnEquipped)
1 Like

It doesn’t look like you’re filtering out which player shot and which didnt.

1 Like

I was basing the code off of some help i got
So im not entirely sure whats wrong
As i dont think they set up any filtering and theirs works

They even have it set up (Somewhat) similar to what i have it set up as
(Mine)
image

(Theirs)
image

Their Code

Server

local t: Tool = script.Parent
local muzzle: Part = t:WaitForChild('Muzzle')::any

local lp: Player = game:GetService('Players').LocalPlayer
local deb: Debris = game:GetService('Debris')
local rep: ReplicatedStorage = game:GetService('ReplicatedStorage')
local uis: UserInputService = game:GetService('UserInputService')
local gunRemote: RemoteEvent = rep:WaitForChild('gun')::any
local param: RaycastParams = RaycastParams.new()
param.FilterDescendantsInstances = {t, lp.Character}
param.FilterType = Enum.RaycastFilterType.Blacklist

t.Activated:Connect(function()
	local cam: Camera = workspace.CurrentCamera
	local mPos: Vector2 = uis:GetMouseLocation()
	local ray: Ray = cam:ViewportPointToRay(mPos.X, mPos.Y)
	
	local rayVec: Vector3 = ray.Direction * 1e3
	local rc: RaycastResult = workspace:Raycast(ray.Origin, rayVec, param)
	gunRemote:FireServer(muzzle, if rc then rc.Position else ray.Origin + rayVec)
end)

local tracerF: Part = workspace:WaitForChild('Tracers')
local tracerWidth: number = 0.1
local tracerClr: Color3 = Color3.new(1, 1, 0)
local function addTracer(from: Vector3, to: Vector3)
	local p: Part = Instance.new('Part')
	p.Anchored = true
	p.CanCollide = false
	p.CanTouch = false
	p.CanQuery = false
	p.Color = tracerClr
	p.Transparency = .5
	p.Material = Enum.Material.Neon
	p.Size = Vector3.new(tracerWidth, tracerWidth, (from - to).Magnitude)
	p.CFrame = CFrame.lookAt(from:Lerp(to, .5), to)
	p.Parent = tracerF
	deb:AddItem(p, .1)
end

gunRemote.OnClientEvent:Connect(addTracer)

Server

local rep: ReplicatedStorage = game:GetService('ReplicatedStorage')
local gunRemote: RemoteEvent = rep:WaitForChild('gun')::any

gunRemote.OnServerEvent:Connect(function(p: Player, muzzle: Part, hit: Vector3)
	local param: RaycastParams = RaycastParams.new()
	param.FilterDescendantsInstances = {muzzle.Parent, p.Character}
	param.FilterType = Enum.RaycastFilterType.Blacklist
	
	local origin: Vector3 = muzzle.CFrame.Position
	local dir: Vector3 = hit - origin
	local hitreg: RaycastResult = workspace:Raycast(origin, dir * 2, param)
	
	local hitPos: Vector3
	if hitreg then
		hitPos = hitreg.Position
		local m: Model? = hitreg.Instance:FindFirstAncestorOfClass('Model')
		if m then
			local hum: Humanoid? = m:FindFirstChild('Humanoid')::any
			if hum then
				hum:TakeDamage(10)
			end
		end
	else
		hitPos = hit
	end
	
	gunRemote:FireAllClients(muzzle.CFrame.Position, hitPos)
end)

(Edit)
Not mine but heres the demo they gave me
gunToolTest.rbxl (44.8 KB)

Fixxed it, No longer need help on this
Moved script and event back into the weapon and just created another event for bullet replication