Weapon that both players and NPCs can use?

  1. **What do you want to achieve?: I want to make my guns in a way so both NPCs and players can use them when they equip them. The guns are scattered around the map in which they can be picked and used by both NPCs and players in runtime instead starting with them.

  2. **What is the issue?: Replication- The bullet is created in a server script on FireCommand. when the gun is equipped by me and I activate then it calls RemoteEvent to Fire() function however the bullet is glitched(the bullet is still in the server) and i want the bullet to be replicated back to the player that shot the bullet local script

  3. **What solutions have you tried so far?: Plenty of youtube videos on how to make a weapon, how to make npcs shoot but not on how to make a dynamic weapon that can be picked up and used by both npcs and players

ServerScript

local Tool = script.Parent
local h
--animations
local idleAnim = {}
idleAnim.AnimClip = script.IdleRifle
idleAnim.AnimTrack = nil
idleAnim.Humanoid = nil
local fireAnim = {}
fireAnim.AnimClip = script.FireRifle
fireAnim.AnimTrack = nil
fireAnim.Humanoid = nil
local reloadAnim = {}
reloadAnim.AnimClip = script.ReloadRifle
reloadAnim.AnimTrack = nil
reloadAnim.Humanoid = nil

local RS = game:GetService("ReplicatedStorage")
local fireWeapon = RS:WaitForChild("Fire")
local reloadWeapon = RS:WaitForChild("ReloadWeapon")

local function PlayAnim(Humanoid : Humanoid,Anim)
	if Humanoid and Humanoid.Health > 0 then
		
		if Anim.Humanoid == nil or Anim.Humanoid ~= Humanoid then
			Anim.Humanoid = Humanoid
			
			if Anim.Humanoid:LoadAnimation(Anim.AnimClip).Length <0.5 then
				Anim.AnimTrack = nil
			end
		end
		if Anim.AnimTrack == nil then
			Anim.AnimTrack = Anim.Humanoid:LoadAnimation(Anim.AnimClip)
			--reloadAnim.AnimTrack.Priority = Enum.AnimationPriority.Action3
			Anim.AnimTrack:Play()
		else
			--reloadAnim.AnimTrack.Priority = Enum.AnimationPriority.Action3
			Anim.AnimTrack:Play()
			
		end
	end
end
local function StopAnim(Humanoid : Humanoid,Anim)
	if Humanoid and Humanoid.Health > 0 then
		if Anim.AnimTrack ~= nil and Anim.AnimTrack.IsPlaying then
			Anim.AnimTrack:Stop()
		end
	end
end
local function Reload()
	if Tool:GetAttribute("IsEquipped") == true and Tool:GetAttribute("CanReload") == true then
		Tool:SetAttribute("CanReload",false)
		
		PlayAnim(h,reloadAnim)
		
		script.Reload:Play()
		
		wait(3)
		local dif = Tool:GetAttribute("ClipSize") - Tool:GetAttribute("CurrentAmmo")
		Tool:SetAttribute("CurrentAmmo", Tool:GetAttribute("CurrentAmmo")+ dif)
		Tool:SetAttribute("CanReload",true)
	end

end
local function Fire()
	--fireWeapon:InvokeClient()
	if Tool:GetAttribute("IsEquipped") == false or Tool:GetAttribute("CanFire") == false then
		return
	end
	if Tool:GetAttribute("CurrentAmmo") > 0 then
		Tool:SetAttribute("CanFire", false)
		Tool:SetAttribute("CurrentAmmo",Tool:GetAttribute("CurrentAmmo")-1) 
		PlayAnim(h,fireAnim)
		local bullet = Instance.new("Part")	
		bullet.CanTouch = true
		bullet.Size = Vector3.new(0.2,0.2,0.5)
		bullet.Position = Tool.M4.Barrel.WorldPosition
		bullet.Orientation = Tool.Handle.Orientation
		bullet.CanCollide = false
		bullet.Touched:Connect(function(hit)
			print(hit)
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum and hum.Parent ~= Tool.Parent then
				hum:TakeDamage(Tool:GetAttribute("Damage"))
				bullet:Destroy()
			else
				bullet:Destroy()
			end
		end)
		bullet.Parent = workspace
		local a1 = Instance.new("Attachment")
		a1.Parent = bullet 
		a1.Position = Vector3.new(-0.1,-0.1,0)
		script.Fire:Play()
		local a2 = Instance.new("Attachment")
		a2.Parent = bullet
		a2.Position = Vector3.new(0.1,0.1,0)
		local a3 = Instance.new("Attachment")
		a3.Parent = bullet
		local t = Instance.new("Trail")
		t.Parent = bullet
		t.Attachment0 = a1
		t.Attachment1 = a2
		t.Color = ColorSequence.new(bullet.Color)
		t.WidthScale = NumberSequence.new(1,0)
		t.Lifetime = 0.3
		t.MinLength = 1
		t.FaceCamera = true
		local bv = Instance.new("LinearVelocity",bullet)
		bv.Attachment0 = a3
		bv.VectorVelocity = Tool.Handle.CFrame.LookVector *100
		wait(Tool:GetAttribute("DelayBetweenShot"))
		Tool:SetAttribute("CanFire", true)
		bullet:SetNetworkOwner(nil)
		
		
		spawn(function()
			wait(5)
			bullet:Destroy()
		end)
	else
		script.Empty:Play()
		wait(Tool:GetAttribute("DelayBetweenShot"))
	end
end
Tool.AttributeChanged:Connect(function(AttributeName)
	if AttributeName == "Fire" and Tool:GetAttribute("Fire") == true  then
		Tool:SetAttribute("Fire",false)
		Fire()
	end
	if AttributeName == "Reload" and Tool:GetAttribute("Reload") == true  then
		Tool:SetAttribute("Reload",false)
		Reload()
	end
end)
Tool.Handle.Touched:Connect(function(hit)
	if Tool:GetAttribute("IsEquipped") == false and hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid").Health > 0 then
		Tool.Parent = hit.Parent
		
	end
end)
Tool.AncestryChanged:Connect(function(child,NewParent)
	
	if NewParent and NewParent:FindFirstChild("Humanoid") and NewParent:FindFirstChild("Humanoid").Health > 0 then
		StopAnim(h,idleAnim)
		idleAnim.AnimTrack = nil
		fireAnim.AnimTrack = nil
		reloadAnim.AnimTrack = nil
		PlayAnim(NewParent:FindFirstChild("Humanoid"),idleAnim)
		h = NewParent:FindFirstChild("Humanoid")
		Tool:SetAttribute("IsEquipped",true)	
	else
		StopAnim(h,idleAnim)
		idleAnim.AnimTrack = nil
		fireAnim.AnimTrack = nil
		reloadAnim.AnimTrack = nil
		Tool:SetAttribute("IsEquipped",false)	
	end
end)
local function FireWeapon()
	Tool:SetAttribute("Fire",true)
end
local function ReloadWeapon()
	Tool:SetAttribute("Reload",true)
end
fireWeapon.OnServerInvoke = FireWeapon
reloadWeapon.OnServerEvent:Connect(ReloadWeapon)

local script

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Mouse = player:GetMouse()
local Tool = script.Parent
local h

local RS = game:GetService("ReplicatedStorage")
local fireWeapon = RS:WaitForChild("Fire")
local reloadWeapon = RS:WaitForChild("ReloadWeapon")

Mouse.Button1Down:Connect(function()
	fireWeapon:InvokeServer()
end)
UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		reloadWeapon:FireServer()
	end
end)