Gun Shoot Problem

Sometimes when I shoot my gun on my player it shoots for the other player. Why is that happening? I’m not sure what I can do to fix it.

Video:Gun problem - YouTube

Local Script:

local Player = game.Players.LocalPlayer
task.wait()
local Character = Player.Character or Player.CharacterAdded:Wait()
local PrimaryPart = Character.PrimaryPart
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local mouse = Player:GetMouse()
--Services
local UserInputService = game:GetService("UserInputService")
local Runservice = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Camera = game.Workspace.CurrentCamera
local ShootRemote = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Shoot")
local hud = Player.PlayerGui:WaitForChild("HUD")
local ArmoryEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("ArmoryEvent")
local ContextActionService = game:GetService("ContextActionService")
--Aiming Stuff

local WalkingTable = {}
local IsWalking = Character:WaitForChild("Config"):WaitForChild("IsWalking")
local IsEquipped = Character:WaitForChild("Config"):WaitForChild("IsEquipped")
local HasAmmo = true
local CurrentKey = false
local gunWalkAnim = nil
local canShoot = true
local IsReloading = false
local debounce = false
local IsAiming = false
local CurrentSwayAmt = .3
local SwayAmt = -.2
local SwayCF = CFrame.new()
local LastCameraCF = CFrame.new()
local fireAnim = nil
local IdleAnim = nil
local shootSound = nil
local isShooting = false
local UserBegan, UserEnded, Runcon
local inputs = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D}
-------
local Framework = {
	inventory = {
		"Smith & Wesson"
	};
	
	module = nil;
	Armory = nil;
	CurrentSlot = nil;
}





 function onRunning(speed)
	if speed > 0 then
		print("Player is running")
		gunWalkAnim:Play()
	else
		print("Player has stopped")
		gunWalkAnim:Stop()
	end
end

	
		


function loadSlot(Item)
	local ArmoryFolder = game:GetService("ReplicatedStorage"):WaitForChild("Armory")
	local ModuleFolder =game.ReplicatedStorage.Modules

	for i, v in pairs(Camera:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
			print("Destroyed")
		end

	end


	if ModuleFolder:FindFirstChild(Item) then
		print("FoundModule")
		Framework.module = require(ModuleFolder:FindFirstChild(Item))




		if ArmoryFolder:FindFirstChild(Item) then
			Framework.Armory = ArmoryFolder:FindFirstChild(Item):Clone()

			local Motor6D = Instance.new("Motor6D")
			Motor6D.Part0 = Character:WaitForChild("RightHand")
			Motor6D.Part1 = Framework.Armory.PrimaryPart
			Motor6D.C0 = CFrame.new(-0.039, -0.202, -0.287) *CFrame.Angles(math.rad(0), math.rad(89.999), math.rad(-101))
			Framework.Armory.Parent =	Camera
			Motor6D.Parent = Character:WaitForChild("RightHand")

			IdleAnim = Instance.new("Animation")
			IdleAnim.Parent = Framework.Armory
			IdleAnim.Name = "GunIdle"
			IdleAnim.AnimationId = Framework.module.idleAnim
			IdleAnim = Humanoid:LoadAnimation(IdleAnim)


			fireAnim = Instance.new("Animation")
			fireAnim.Parent = Framework.Armory
			fireAnim.Name = "Fire"
			fireAnim.AnimationId = Framework.module.fireAnim
			fireAnim = Humanoid:LoadAnimation(fireAnim)
			
			
			gunWalkAnim = Instance.new("Animation")
			gunWalkAnim.Parent = Framework.Armory
			gunWalkAnim.Name = "GunWalk"
			gunWalkAnim.AnimationId = Framework.module.idleWalkAnim
			gunWalkAnim = Humanoid:LoadAnimation(gunWalkAnim)
			
			
			shootSound = Instance.new("Sound")
			shootSound.Parent = HumanoidRootPart
			shootSound.Name = "ShootSound"
			shootSound.SoundId = Framework.module.shootSound

			--Adding Sight For Aiming With Gun
		end
	end
end

function CameraAimTween(ModuleTime)


	local Info  = TweenInfo.new(
		ModuleTime,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)

	if IsAiming == true then

		TweenService:Create(Camera, Info, {FieldOfView = 70}):Play()
	else
		TweenService:Create(Camera, Info, {FieldOfView = 100}):Play()
	end
end


	
	Character = Player.Character or Player.CharacterAdded:Wait()
	
	print("Remote is being fired again")
	print(Player)

	IsEquipped.Value = false
	
	print(IsEquipped.Value)
	
	--[[Framework = {
		inventory = {
			"Smith & Wesson"
		};

		module = nil;
		Armory = nil;
		CurrentSlot = nil;
	}]]
	
	 Player = game.Players.LocalPlayer
	task.wait()
	Character = Player.Character or Player.CharacterAdded:Wait()
	 Humanoid = Character:WaitForChild("Humanoid")
	HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	mouse = Player:GetMouse()
	
--[[	if Camera:FindFirstChildWhichIsA("Model") then
		Camera:FindFirstChildWhichIsA("Model"):Destroy()
	end]]


	
	



	Runservice.RenderStepped:Connect(function()

		mouse.TargetFilter = Framework.Armory
		
		if Humanoid then
				
			if Framework.Armory ~= nil  and Framework.module ~=nil   then
				hud = Player.PlayerGui:WaitForChild("HUD")
				hud.Enabled = true
				hud["Firearm[NAME]"].Text = Framework.inventory[Framework.CurrentSlot]
				hud["Firearm[TYPE]"].Text = Framework.module.Class
				hud.Ammo.Text = Framework.module.maxAmmo
				hud.Ammo:WaitForChild("MagsAmmo").Text = Framework.module.CurrentAmmo
			
			
			end
		end



	end)


	
	UserBegan  =	UserInputService.InputBegan:Connect(function(input, GPE)

		if GPE then return end

	if input.KeyCode == Enum.KeyCode.E  and IsEquipped.Value == false and IsWalking.Value == false  then
		
		IsEquipped.Value = true
			loadSlot(Framework.inventory[1])
			Framework.CurrentSlot = 1
			ArmoryEvent:FireServer("Equip", mouse.Hit, mouse.Target, mouse)
	end
	
	
		if input.UserInputType == Enum.UserInputType.MouseButton2 and Framework.module.CanAim and IsEquipped.Value == true then

			IsAiming = true
			

			CameraAimTween(Framework.module.AimingTime)
			print("Aiming")
			
		end
	if input.UserInputType == Enum.UserInputType.MouseButton1  then
		ArmoryEvent:FireServer("Shooting", mouse.Hit, mouse.Target, mouse)
		if Framework.module.CurrentAmmo  ~= 0 then
		
		if HasAmmo == false then
			print("no bullets left")
			
		else
			
			Framework.module.CurrentAmmo -= 1
		end
			
		
			end
		end
	
	if table.find(inputs, input.KeyCode) and IsEquipped.Value == true and CurrentKey == false then
		CurrentKey = true
		IsWalking.Value = false
		gunWalkAnim:Play()
		print("something occured")
	end
	

	if input.KeyCode == Enum.KeyCode.R and  IsReloading == false and IsEquipped.Value == true then

		canShoot = false
		IsReloading = true
		ArmoryEvent:FireServer("Reload", mouse.Hit, mouse.Target, mouse)
		task.wait(Framework.module.reloadTime)

		canShoot = true
		IsReloading = false

		if Framework.module.maxAmmo ~= 0 then
			
			Framework.module.maxAmmo -=1
			Framework.module.CurrentAmmo = Framework.module.ammo
			
			else 
		
			print("Out of Ammo Client")
		
			end
		end
	end)


	UserEnded = UserInputService.InputEnded:Connect(function(input, GPE)
		if GPE then return end

		if input.UserInputType == Enum.UserInputType.MouseButton2 and Framework.module.CanAim and IsEquipped.Value == true then
		
		CameraAimTween(Framework.module.AimingTime)
		print("Stopped Aiming")
		
			IsAiming = false
			
		end
	
	--[[if input.KeyCode == Enum.KeyCode.W  and IsWalking.Value == false then
		
		IsWalking.Value = true
		gunWalkAnim:Stop()
	end]]
	
	if table.find(inputs, input.KeyCode)  and CurrentKey == true then
		CurrentKey = false
		IsWalking.Value = true
		gunWalkAnim:Stop()
		print("ha")
	end
	

	end)
	
	
	
	
	Humanoid.Died:Connect(function()
		UserBegan:Disconnect()
		UserEnded:Disconnect()
		--IsEquipped.Value = false
		--game.ReplicatedStorage.Events.PlayerAdded:FireServer()
		hud.Enabled = false
		hud["Firearm[NAME]"].Text = " "
		hud["Firearm[TYPE]"].Text = " "
		hud.Ammo.Text = " "
		hud.Ammo:WaitForChild("MagsAmmo").Text = " "
		
	

	end)


Server Script:

--Systems
local UserInputService = game:GetService("UserInputService")
local Runservice = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Camera = game.Workspace.CurrentCamera
local ShootRemote = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Shoot")
local ArmoryEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("ArmoryEvent")
local Camera = game.Workspace.CurrentCamera
---- Variables
local canShoot = true
local IsReloading = false
local gunWalkAnim = nil
local fireAnim = nil
local IdleAnim = nil
local equipAnim = nil
local  unEquipAnim = nil
local shootSound = nil
local IsAiming = false
local debounce = false


----


local Params = RaycastParams.new()
local range = 300
local filterTable = {}

--Armory Table
local Framework = {
	inventory = {
		"Smith & Wesson"
	};

	module = nil;
	Armory = nil;
	CurrentSlot = nil;
}

local function RayCastDetection(Localplayer, hit, MouseTarget,HeadShotDmg, RegularDmg, Mouse)
--	Mouse.TargetFilter = Framework.Armory
	
	local HumanoidRootPart = Localplayer.Character.HumanoidRootPart
	
	local Origin = HumanoidRootPart.Position
	local End = (hit.Position - Origin).Unit * range
	local Result = workspace:Raycast(Origin, End, Params)
	
	if Result then
		local target = Result.Instance
		
	
		
			local Humanoid = target.Parent:FindFirstChild("Humanoid")
			
		if Humanoid then
			
						if target.Name == "Head" then
							Humanoid:TakeDamage(HeadShotDmg)
							print("HeadShot")
							fireAnim:Play()
							shootSound:Play()
							
							Framework.Armory.Muzzle.Smoke.Enabled = true
							Framework.Armory.Muzzle.Flash.Enabled = true
							task.wait(.5)
							Framework.Armory.Muzzle.Flash.Enabled = false
							task.wait(1)
							Framework.Armory.Muzzle.Smoke.Enabled = false
						else
							Humanoid:TakeDamage(RegularDmg)
							print("BodyShot")
							fireAnim:Play()
							shootSound:Play()
							
							Framework.Armory.Muzzle.Smoke.Enabled = true
							Framework.Armory.Muzzle.Flash.Enabled = true
							task.wait(.5)
							Framework.Armory.Muzzle.Flash.Enabled = false
							task.wait(1)
							Framework.Armory.Muzzle.Smoke.Enabled = false
						end
						table.insert(filterTable, target)
						Params.FilterDescendantsInstances = filterTable
						return RayCastDetection(hit)
					end
				end
			end






local function OnShoot(Localplayer, hit, target,HeadShotDmg, RegularDmg, Mouse)

	local Char = Localplayer.Character
	filterTable = {}

	table.insert(filterTable, Localplayer.Character)
	Params.FilterDescendantsInstances = filterTable
	Params.FilterType = Enum.RaycastFilterType.Blacklist

	RayCastDetection(Localplayer, hit, target, HeadShotDmg, RegularDmg, Mouse)

	fireAnim:Play()
	shootSound:Play()

	Framework.module.CurrentAmmo -=1
	Framework.Armory.Muzzle.Smoke.Enabled = true
	Framework.Armory.Muzzle.Flash.Enabled = true
	task.wait(.5)
	Framework.Armory.Muzzle.Flash.Enabled = false
	task.wait(1)
	Framework.Armory.Muzzle.Smoke.Enabled = false
	
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		local IsWalking = Instance.new("BoolValue")
		IsWalking.Name = "IsWalking"
		IsWalking.Value = false
		IsWalking.Parent = player.Character:WaitForChild("Config")
		
		function loadSlot(Item, MyCharacter)
			local ArmoryFolder = game:GetService("ReplicatedStorage").Armory
			local ModuleFolder =game.ReplicatedStorage.Modules
			
			local Folder = Instance.new("Folder")
			Folder.Name = "ArmoryFolder"
			Folder.Parent = MyCharacter["RightHand"]
			
			
		--[[	for i, v in pairs(MyCharacter:GetChildren()) do
				if v:IsA("Model") and v.Name == Item  then
					v:Destroy()
				end

			end]]
			
		

			if ModuleFolder:FindFirstChild(Item) then
				print("FoundModule")
				Framework.module = require(ModuleFolder:FindFirstChild(Item))




				if ArmoryFolder:FindFirstChild(Item) then
					Framework.Armory = ArmoryFolder:FindFirstChild(Item):Clone()

				
					
					local Motor6D = Instance.new("Motor6D")
					Motor6D.Part0 = MyCharacter:WaitForChild("RightHand")
					Motor6D.Part1 = Framework.Armory.PrimaryPart
					Motor6D.C0 = CFrame.new(-0.039, -0.202, -0.287) *CFrame.Angles(math.rad(0), math.rad(89.999), math.rad(-101))
					Framework.Armory.Parent =	Folder
					Motor6D.Parent = MyCharacter:WaitForChild("RightHand")

					IdleAnim = Instance.new("Animation")
					IdleAnim.Parent = Framework.Armory
					IdleAnim.Name = "GunIdle"
					IdleAnim.AnimationId = Framework.module.idleAnim
					IdleAnim = MyCharacter.Humanoid:LoadAnimation(IdleAnim)


					fireAnim = Instance.new("Animation")
					fireAnim.Parent = Framework.Armory
					fireAnim.Name = "Fire"
					fireAnim.AnimationId = Framework.module.fireAnim
					fireAnim =  MyCharacter.Humanoid:LoadAnimation(fireAnim)
					
					
					equipAnim = Instance.new("Animation")
					equipAnim.Parent = Framework.Armory
					equipAnim.Name = "Equip"
					equipAnim.AnimationId = Framework.module.equipAnim
					equipAnim = MyCharacter.Humanoid:LoadAnimation(equipAnim)
					
					
					unEquipAnim = Instance.new("Animation")
					unEquipAnim.Parent = Framework.Armory
					unEquipAnim.Name = "Unequip"
					unEquipAnim.AnimationId = Framework.module.unequipAnim
					unEquipAnim = MyCharacter.Humanoid:LoadAnimation(unEquipAnim)
					
					
					gunWalkAnim = Instance.new("Animation")
					gunWalkAnim.Parent = Framework.Armory
					gunWalkAnim.Name = "GunWalk"
					gunWalkAnim.AnimationId = Framework.module.idleWalkAnim
					gunWalkAnim = MyCharacter.Humanoid:LoadAnimation(gunWalkAnim)

					shootSound = Instance.new("Sound")
					shootSound.Parent = MyCharacter.HumanoidRootPart
					shootSound.Name = "ShootSound"
					shootSound.SoundId = Framework.module.shootSound

					print("Found Armory")
					--Adding Sight For Aiming With Gun
				end
			end
		end
		---Armory Coding

		Framework = {
			inventory = {
				"Smith & Wesson"
			};

			module = nil;
			Armory = nil;
			CurrentSlot = nil;
		}
		
		if Camera:FindFirstChildWhichIsA("Model") then
			Camera:FindFirstChildWhichIsA("Model"):Destroy()
		end
		----

	end)
end)

ArmoryEvent.OnServerEvent:Connect(function(MyPlayer, info, MouseHit, MouseTarget, Mouse)
	local IsEquipped = MyPlayer.Character:WaitForChild("Config"):WaitForChild("IsEquipped")
	local Humanoid = MyPlayer.Character:WaitForChild("Humanoid")
	

	if info == "Equip" then

		if IsEquipped.Value == false then
			IsEquipped.Value = true
			ArmoryEvent:FireClient(MyPlayer, "Equipped")
			loadSlot(Framework.inventory[1], MyPlayer.Character)
			Framework.CurrentSlot = 1
			print("Slot 1")
			

			equipAnim:Play()

			task.wait(.5)
			IdleAnim:Play()
			equipAnim:Stop()
			
			print("Equipped")
			
			
		
		elseif IsEquipped.Value == true then
			IsEquipped.Value = false
			Framework.CurrentSlot = 1 
			print("unequipped")
			
			

			local ArmoryFolder = MyPlayer.Character["RightHand"].ArmoryFolder
			ArmoryFolder:Destroy()



			ArmoryEvent:FireClient(MyPlayer, "Unequipped")

			unEquipAnim:Play()
			task.wait()
			
			IdleAnim:Stop()
			
		
		end



	elseif info == "AimingScreen" and IsEquipped.Value == true  then

		if Framework.module.CanAim and IsEquipped.Value == true then
			
			print("Aiming")

		end
	elseif info == "Shooting" then
		if Framework.Armory ~= nil and Framework.module.ammo ~= 0 and debounce == false and IsReloading ~= true and canShoot == true and IsEquipped.Value == true then

			if Framework.module.CurrentAmmo == 0 then
				print("Reload!")
			else
				print( "Shoot")
				OnShoot(MyPlayer,MouseHit, MouseTarget, Framework.module.headShot, Framework.module.damage, Mouse)
			end


			task.wait(Framework.module.debounce)

			debounce = false
		end

	elseif info == "Reload" then
		if IsReloading == false and IsEquipped.Value == true then

			canShoot = false
			IsReloading = true

			task.wait(Framework.module.reloadTime)

			canShoot = true
			IsReloading = false

			if Framework.module.maxAmmo ~= 0 then
				Framework.module.maxAmmo -=1
				Framework.module.CurrentAmmo = Framework.module.ammo
				
			else
				
				print("Out of ammo!"..MyPlayer.Name)
			end

		elseif info == "AimingScreenEnded" and Framework.module.CanAim and IsEquipped.Value == true then


			IsAiming = false
	
			print("Stopped Aiming")

	end
	end
	
	Humanoid.Died:Connect(function()
		IsEquipped.Value = false

	end)
end)