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

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
