function HolsterWeapon(A,O,V,p)if A.Character:FindFirstChild(V.HolsterPoint)then local c=V.HolsterPoint local Q=(GunModels:FindFirstChild(O)):Clone()Q.Name="\083\095"..O Q.Parent=A.Character if Q:FindFirstChild("\078\111\100\101\115")then Q.Nodes:Destroy()end local Z=p:FindFirstChild("\082\101\112\086\097\108\117\101\115")for A,O in pairs(Q:GetChildren())do if O:IsA("\066\097\115\101\080\097\114\116")and O.Name~="\072\097\110\100\108\101"then if O.Name=="\083\105\103\104\116\077\097\114\107"or O.Name=="\087\097\114\104\101\097\100"and(Z and Z.Mag.Value<1)then O:Destroy()else local A=Ultil.WeldComplex(Q.Handle,O,O.Name)A.Parent=Q.Handle O.Anchored=false O.CanCollide=false end end end local x=Ultil.Weld(A.Character[c],Q.Handle,V.HolsterCFrame)x.Parent=Q x.Name="\072\111\108\115\116\101\114\087\101\108\100"Q.Handle.Anchored=false end end function SpawnGun(A,O,V,p,c)local Q=(GunModels:FindFirstChild(A)):Clone()Q.Handle.Anchored=false Q.Handle.CanTouch=true Q.PrimaryPart=Q.Handle Q.Handle.Size=Q:GetExtentsSize()if Q:FindFirstChild("\078\111\100\101\115")then Q.Nodes:Destroy()end if#Q:GetChildren()<2 then Q.Handle.CanCollide=true else Q.Handle.CanCollide=false end for A,O in pairs(Q:GetChildren())do if O.Name=="\087\097\114\104\101\097\100"and(c and(c.IsLauncher and(V:FindFirstChild("\082\101\112\086\097\108\117\101\115")and V.RepValues.Mag.Value<1)))then O:Destroy()elseif O:IsA("\066\097\115\101\080\097\114\116")and O.Name~="\072\097\110\100\108\101"then local A=Ultil.WeldComplex(Q.Handle,O,O.Name)A.Parent=Q.Handle O.Anchored=false O.CanCollide=true O.CanTouch=false PhysService:SetPartCollisionGroup(O,"\071\117\110\115")end end if not V then V=(Engine.ToolStorage:FindFirstChild(A)):Clone()end V.Parent=Q local Z=Instance.new("\067\108\105\099\107\068\101\116\101\099\116\111\114")Z.MaxActivationDistance=gameRules.PickupDistance Z.Parent=Q Z.MouseClick:Connect(function(A)V.Parent=A.Backpack Q:Destroy()local O=Engine.FX.WeaponPickup:Clone()O.Parent=A.Character.Torso O:Play()O.PlayOnRemove=true O:Destroy()end)Q.Parent=ACS_Workspace.DroppedGuns Q.Handle.Touched:Connect(function()if Q.Handle.AssemblyLinearVelocity.Magnitude>7 then local A=Engine.FX.GunDrop local O=A["\071\117\110\068\114\111\112"..math.random(#A:GetChildren())]:Clone()O.Parent=Q.Handle O.PlaybackSpeed=math.random(30,50)/40 O:Play()O.PlayOnRemove=true O:Destroy()end end)if p then Q.Handle:SetNetworkOwner(p)end Q:SetPrimaryPartCFrame(O)if#ACS_Workspace.DroppedGuns:GetChildren()>gameRules.MaxDroppedWeapons then(ACS_Workspace.DroppedGuns:GetChildren())[1]:Destroy()end if gameRules.TimeDespawn then Debris:AddItem(Q,gameRules.WeaponDespawnTime)end return Q end Evt.Shell.OnServerEvent:Connect(function(A,O,V)Evt.Shell:FireAllClients(O,V)end)Evt.DropWeapon.OnServerEvent:Connect(function(A,O,V)local p=A.Backpack:FindFirstChild(O.Name)local c=Engine.FX.WeaponDrop:Clone()c.Parent=A.Character.Torso c:Play()c.PlayOnRemove=true c:Destroy()SpawnGun(p.Name,A.Character.Torso.CFrame*CFrame.new(0,1,-3),p,A,V)end)Evt.RepAmmo.OnServerEvent:Connect(function(A,O,V,p,c)local Q=O.RepValues Q.StoredAmmo.Value=p Q.Mag.Value=V Q.Chambered.Value=c if O.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")and O.Parent:FindFirstChild("\083"..O.Name)then for A,O in pairs(O.Parent["\083"..O.Name]:GetChildren())do if O.Name=="\087\097\114\104\101\097\100"then if V>0 then O.Transparency=0 else O.Transparency=1 end end end end end)Evt.DropAmmo.OnServerEvent:Connect(function(A,O,V)if V=="\087\101\108\100"then local A=Engine.AmmoModels.AmmoBox:Clone()local V=O.Handle for V,p in pairs(A:GetChildren())do if p:IsA("\066\097\115\101\080\097\114\116")and p.Name~="\077\097\105\110"then local O=Ultil.WeldComplex(A.Main,p,p.Name)O.Parent=A.Main p.Anchored=false p.CanCollide=true p.CanTouch=false end PhysService:SetPartCollisionGroup(p,"\071\117\110\115")if p.Name=="\077\097\105\110"then for A,V in pairs(p:GetChildren())do if V:FindFirstChildWhichIsA("\084\101\120\116\076\097\098\101\108")then(V:FindFirstChildWhichIsA("\084\101\120\116\076\097\098\101\108")).Text=O.AmmoType.Value end end end end local p=Ultil.Weld(V,A.Main,CFrame.new(0,-0.2,0),CFrame.new())p.Name="\084\111\111\108\087\101\108\100"p.Parent=V A.Main.Anchored=false V.Anchored=false A.Parent=O elseif V=="\068\101\115\116\114\111\121"then if O:FindFirstChildWhichIsA("\077\111\100\101\108")then(O:FindFirstChildWhichIsA("\077\111\100\101\108")):Destroy()O.Handle.ToolWeld:Destroy()end elseif V=="\068\114\111\112"then local A=O:FindFirstChildWhichIsA("\077\111\100\101\108")local V=O.Handle V.ToolWeld:Destroy()A.Parent=ACS_Workspace.DroppedGuns A.Main.Touched:Connect(function(V)if plr:GetPlayerFromCharacter(V.Parent)then local p=plr:GetPlayerFromCharacter(V.Parent)local c=p.Backpack:GetChildren()for V=1,#c,1 do if c[V]:IsA("\084\111\111\108")and c[V]:FindFirstChild("\065\067\083\095\083\101\116\116\105\110\103\115")then if O.AmmoType.Value=="\085\110\105\118\101\114\115\097\108"then Evt.Refil:FireClient(p,c[V],O.Inf.Value,O.Stored)if not A.Main.Sound.Playing then A.Main.Sound:Play()end elseif(require(c[V].ACS_Settings)).BulletType==O.AmmoType.Value then Evt.Refil:FireClient(p,c[V],O.Inf.Value,O.Stored)if not A.Main.Sound.Playing then A.Main.Sound:Play()end end end end if O.Stored.Value<=0 and not O.Inf.Value then A:Destroy()O:Destroy()return end end end)O.Parent=nil local p=Instance.new("\067\108\105\099\107\068\101\116\101\099\116\111\114",A)p.MaxActivationDistance=gameRules.PickupDistance p.MouseClick:Connect(function(V)O.Parent=V:WaitForChild("\066\097\099\107\112\097\099\107")A:Destroy()end)Debris:AddItem(A,gameRules.AmmoBoxDespawn)end end)for A,O in pairs(ACS_Workspace.WeaponSpawners:GetChildren())do local V=Instance.new("\068\111\117\098\108\101\067\111\110\115\116\114\097\105\110\101\100\086\097\108\117\101")local p=O.Config.WaitTime V.Name="\087\097\105\116\084\105\109\101"V.MaxValue=p.Value V.Value=p.Value V.Parent=O.Config p:Destroy()end
-- Weld holster model to the player
function HolsterWeapon(player, weaponName, toolSettings, tool)
	if player.Character:FindFirstChild(toolSettings.HolsterPoint) then
		local holsterPoint = toolSettings.HolsterPoint
		local holsterModel = GunModels:FindFirstChild(weaponName):Clone()
		holsterModel.Name = "S_" .. weaponName
		holsterModel.Parent = player.Character

		if holsterModel:FindFirstChild("Nodes") then
			holsterModel.Nodes:Destroy()
		end

		local config = tool:FindFirstChild("RepValues")

		for _, part in pairs(holsterModel:GetChildren()) do
			if part:IsA("BasePart") and part.Name ~= "Handle" then
				if part.Name == "SightMark" or (part.Name == "Warhead" and config and config.Mag.Value < 1) then
					part:Destroy()
				else
					local newWeld = Ultil.WeldComplex(holsterModel.Handle, part, part.Name)
					newWeld.Parent = holsterModel.Handle
					part.Anchored = false
					part.CanCollide = false
				end
			end
		end

		local holsterWeld = Ultil.Weld(player.Character[holsterPoint], holsterModel.Handle, toolSettings.HolsterCFrame)
		holsterWeld.Parent = holsterModel
		holsterWeld.Name = "HolsterWeld"
		holsterModel.Handle.Anchored = false
	end
end

function SpawnGun(gunName, gunPosition, tool, player, config)
	local dropModel = GunModels:FindFirstChild(gunName):Clone()
	dropModel.Handle.Anchored = false
	dropModel.Handle.CanTouch = true
	--dropModel.Handle.CFrame = CFrame.new(dropModel["Origin Position"])

	dropModel.PrimaryPart = dropModel.Handle
	dropModel.Handle.Size = dropModel:GetExtentsSize()

	if dropModel:FindFirstChild("Nodes") then
		dropModel.Nodes:Destroy()
	end

	if #dropModel:GetChildren() < 2 then
		dropModel.Handle.CanCollide = true
	else
		dropModel.Handle.CanCollide = false
	end

	for _, part in pairs(dropModel:GetChildren()) do
		if
			part.Name == "Warhead"
			and config
			and config.IsLauncher
			and tool:FindFirstChild("RepValues")
			and tool.RepValues.Mag.Value < 1
		then
			part:Destroy()
		elseif part:IsA("BasePart") and part.Name ~= "Handle" then
			local newWeld = Ultil.WeldComplex(dropModel.Handle, part, part.Name)
			newWeld.Parent = dropModel.Handle
			part.Anchored = false
			part.CanCollide = true
			part.CanTouch = false
			PhysService:SetPartCollisionGroup(part, "Guns")
		end
	end

	if not tool then
		tool = Engine.ToolStorage:FindFirstChild(gunName):Clone()
	end
	tool.Parent = dropModel

	local clickDetector = Instance.new("ClickDetector")
	clickDetector.MaxActivationDistance = gameRules.PickupDistance
	clickDetector.Parent = dropModel
	clickDetector.MouseClick:Connect(function(clicker)
		tool.Parent = clicker.Backpack
		dropModel:Destroy()

		local NewSound = Engine.FX.WeaponPickup:Clone()
		NewSound.Parent = clicker.Character.Torso
		--NewSound.PlaybackSpeed = math.random(30,50)/40
		NewSound:Play()
		NewSound.PlayOnRemove = true
		NewSound:Destroy()
	end)

	dropModel.Parent = ACS_Workspace.DroppedGuns

	dropModel.Handle.Touched:Connect(function()
		if dropModel.Handle.AssemblyLinearVelocity.Magnitude > 7 then
			local DropSounds = Engine.FX.GunDrop
			local NewSound = DropSounds["GunDrop" .. math.random(#DropSounds:GetChildren())]:Clone()
			NewSound.Parent = dropModel.Handle
			NewSound.PlaybackSpeed = math.random(30, 50) / 40
			NewSound:Play()
			NewSound.PlayOnRemove = true
			NewSound:Destroy()
		end
	end)

	if player then
		dropModel.Handle:SetNetworkOwner(player)
	end

	dropModel:SetPrimaryPartCFrame(gunPosition)

	if #ACS_Workspace.DroppedGuns:GetChildren() > gameRules.MaxDroppedWeapons then
		ACS_Workspace.DroppedGuns:GetChildren()[1]:Destroy()
	end

	if gameRules.TimeDespawn then
		Debris:AddItem(dropModel, gameRules.WeaponDespawnTime)
	end

	return dropModel
end

Evt.Shell.OnServerEvent:Connect(function(Player, Shell, Origin)
	Evt.Shell:FireAllClients(Shell, Origin)
end)

Evt.DropWeapon.OnServerEvent:Connect(function(player, tool, toolConfig)
	local tool = player.Backpack:FindFirstChild(tool.Name)
	--print(player.Name.. " dropped a " ..tool.Name)
	--tool:Destroy()
	local NewSound = Engine.FX.WeaponDrop:Clone()
	NewSound.Parent = player.Character.Torso
	--NewSound.PlaybackSpeed = math.random(30,50)/40
	NewSound:Play()
	NewSound.PlayOnRemove = true
	NewSound:Destroy()
	SpawnGun(tool.Name, player.Character.Torso.CFrame * CFrame.new(0, 1, -3), tool, player, toolConfig)
end)

Evt.RepAmmo.OnServerEvent:Connect(function(Player, tool, mag, ammo, chambered)
	local config = tool.RepValues
	config.StoredAmmo.Value = ammo
	config.Mag.Value = mag
	config.Chambered.Value = chambered

	if tool.Parent:FindFirstChild("Humanoid") and tool.Parent:FindFirstChild("S" .. tool.Name) then
		-- Tool is equipped
		for _, part in pairs(tool.Parent["S" .. tool.Name]:GetChildren()) do
			if part.Name == "Warhead" then
				if mag > 0 then
					part.Transparency = 0
				else
					part.Transparency = 1
				end
			end
		end
	end
end)

Evt.DropAmmo.OnServerEvent:Connect(function(Player, tool, action)
	if action == "Weld" then
		local canModel = Engine.AmmoModels.AmmoBox:Clone()
		local handle = tool.Handle
		for _, part in pairs(canModel:GetChildren()) do
			if part:IsA("BasePart") and part.Name ~= "Main" then
				local newWeld = Ultil.WeldComplex(canModel.Main, part, part.Name)
				newWeld.Parent = canModel.Main
				part.Anchored = false
				part.CanCollide = true
				part.CanTouch = false
			end

			PhysService:SetPartCollisionGroup(part, "Guns")

			if part.Name == "Main" then
				for _, child in pairs(part:GetChildren()) do
					if child:FindFirstChildWhichIsA("TextLabel") then
						child:FindFirstChildWhichIsA("TextLabel").Text = tool.AmmoType.Value
					end
				end
			end
		end
		local newWeld = Ultil.Weld(handle, canModel.Main, CFrame.new(0, -0.2, 0), CFrame.new())
		newWeld.Name = "ToolWeld"
		newWeld.Parent = handle
		canModel.Main.Anchored = false
		handle.Anchored = false
		canModel.Parent = tool
	elseif action == "Destroy" then
		if tool:FindFirstChildWhichIsA("Model") then
			tool:FindFirstChildWhichIsA("Model"):Destroy()
			tool.Handle.ToolWeld:Destroy()
		end
	elseif action == "Drop" then
		local canModel = tool:FindFirstChildWhichIsA("Model")
		local handle = tool.Handle
		handle.ToolWeld:Destroy()
		canModel.Parent = ACS_Workspace.DroppedGuns
		canModel.Main.Touched:Connect(function(hitPart)
			if plr:GetPlayerFromCharacter(hitPart.Parent) then
				local player = plr:GetPlayerFromCharacter(hitPart.Parent)
				local f = player.Backpack:GetChildren()
				for i = 1, #f do
					if f[i]:IsA("Tool") and f[i]:FindFirstChild("ACS_Settings") then
						if tool.AmmoType.Value == "Universal" then
							Evt.Refil:FireClient(player, f[i], tool.Inf.Value, tool.Stored)
							if not canModel.Main.Sound.Playing then
								canModel.Main.Sound:Play()
							end
						elseif require(f[i].ACS_Settings).BulletType == tool.AmmoType.Value then
							Evt.Refil:FireClient(player, f[i], tool.Inf.Value, tool.Stored)
							if not canModel.Main.Sound.Playing then
								canModel.Main.Sound:Play()
							end
						end
					end
				end

				-- No more ammo
				if tool.Stored.Value <= 0 and not tool.Inf.Value then
					canModel:Destroy()
					tool:Destroy()
					return
				end
			end
		end)
		tool.Parent = nil

		local clicker = Instance.new("ClickDetector", canModel)
		clicker.MaxActivationDistance = gameRules.PickupDistance
		clicker.MouseClick:Connect(function(Player)
			--print("Give")
			tool.Parent = Player:WaitForChild("Backpack")
			canModel:Destroy()
		end)

		Debris:AddItem(canModel, gameRules.AmmoBoxDespawn)
	end
end)

for _, spawner in pairs(ACS_Workspace.WeaponSpawners:GetChildren()) do
	local constrainedValue = Instance.new("DoubleConstrainedValue")
	local maxTime = spawner.Config.WaitTime

	constrainedValue.Name = "WaitTime"
	constrainedValue.MaxValue = maxTime.Value
	constrainedValue.Value = maxTime.Value
	constrainedValue.Parent = spawner.Config

	maxTime:Destroy()
end
