REMOTES.Reload.OnServerEvent:connect(function(l,b)local A=l.Character if A then local L=A.Equipped local K=A.Ammo if L.Value==b then local A=CONFIG:GetConfig(b)local a=K[A.Size]local Z=b.Ammo if a.Value>0 then if cancels[b]then cancels[b]=nil end local K=A.Magazine for A,L in pairs(Players:GetPlayers())do if L~=l then REMOTES.Effect:FireClient(L,"\082\101\108\111\097\100",b)end end local M=K-Z.Value local R=tick()local m=0 repeat m=tick()-R RunService.Stepped:wait()until m>=A.ReloadTime or L.Value~=b or cancels[b]if L.Value==b then if m>=A.ReloadTime then if a.Value>=M then Z.Value=Z.Value+M a.Value=a.Value-M else Z.Value=Z.Value+a.Value a.Value=0 end end end if cancels[b]then cancels[b]=nil end end end end end)REMOTES.Hit.OnServerEvent:connect(function(l,b,A)spawn(function()local L=shots[l]if L then local K=l.Character local a=K.Equipped if a.Value==L.Item then local K=b.Parent:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if K then if DAMAGE:PlayerCanDamage(l,K)then local a=L.Position local Z=L.Directions[A]local M=L.Config if Z then local R=Ray.new(a,Z)local m=(b.Position-a).Magnitude local j={}for l,b in pairs(Players:GetPlayers())do if b.Character then table.insert(j,b.Character)end end if m<=M.Range then local m=R:Distance(b.Position)if m<15 then if K.Health>0 then L.Directions[A]=nil local R=0 for l,b in pairs(L.Directions)do if b then R=R+1 end end if R==0 then shots[l]=nil end local m=K:FindFirstChild("\068\111\119\110")local j=false if m then j=m.Value end local P=DAMAGE:Calculate(L.Item,b,a)DAMAGE:Damage(K,P,l)local s=Players:GetPlayerFromCharacter(K.Parent)if s then REMOTES.HitIndicator:FireClient(s,Z,P)end if K.Health<=0 then for l,b in pairs(K.Parent:GetChildren())do if b:IsA("\066\097\115\101\080\097\114\116")then b.Velocity=Z*M.Damage end end local b=math.floor((a-K.Parent.HumanoidRootPart.Position).Magnitude+.5)STAT_SCRIPT.FurthestKill:Fire(l,b)REMOTES.Killfeed:FireAllClients(l.Name,K.Parent.Name,L.Item.Name,b)end end end end end end end end end end)end)REMOTES.Shoot.OnServerEvent:connect(function(l,b,A,L)if shots[l]then shots[l]=nil end local K=l.Character local a=K.HumanoidRootPart local Z=K.Humanoid local M=Z.Down local R=K.Equipped local m=K.Items if b.Parent==m and not M.Value then if(a.Position-A).Magnitude<15 then if R.Value==b then local K=b.Ammo local a=CONFIG:GetConfig(b)if#L==a.ShotSize then if K.Value>0 then K.Value=K.Value-1 for l,b in pairs(L)do L[l]=b.Unit end local Z={Item=b,Config=a,Position=A,Directions=L}shots[l]=Z cancels[b]=true for K,a in pairs(Players:GetPlayers())do if a~=l then REMOTES.Effect:FireClient(a,"\083\104\111\111\116",b,A,L)end end end end end end end end)Players.PlayerRemoving:connect(function(l)if shots[l]then shots[l]=nil end end)


REMOTES.Reload.OnServerEvent:connect(function(player, item)
	local character	= player.Character
	
	if character then
		local equipped	= character.Equipped
		local ammo		= character.Ammo
		if equipped.Value == item then
			local config		= CONFIG:GetConfig(item)
			local storedAmmo	= ammo[config.Size]
			local itemAmmo		= item.Ammo
			
			if storedAmmo.Value > 0 then
				if cancels[item] then
					cancels[item]	= nil
				end
				local magazine	= config.Magazine
				
				for _, p in pairs(Players:GetPlayers()) do
					if p ~= player then
						REMOTES.Effect:FireClient(p, "Reload", item)
					end
				end
				
				local needed	= magazine - itemAmmo.Value
				local start		= tick()
				local elapsed	= 0
				
				repeat
					elapsed	= tick() - start
					RunService.Stepped:wait()
				until elapsed >= config.ReloadTime or equipped.Value ~= item or cancels[item]
				
				if equipped.Value == item then
					if elapsed >= config.ReloadTime then
						if storedAmmo.Value >= needed then
							itemAmmo.Value		= itemAmmo.Value + needed
							storedAmmo.Value	= storedAmmo.Value - needed
						else
							itemAmmo.Value		= itemAmmo.Value + storedAmmo.Value
							storedAmmo.Value	= 0
						end
					end
				end
				
				if cancels[item] then
					cancels[item]	= nil
				end
			end
		end
	end
end)

REMOTES.Hit.OnServerEvent:connect(function(player, hit, index)
	spawn(function() -- spawn to avoid race conditions
		local shot	= shots[player]
		
		if shot then
			local character	= player.Character
			local equipped	= character.Equipped
			
			if equipped.Value == shot.Item then
				local humanoid	= hit.Parent:FindFirstChildOfClass("Humanoid")
				if humanoid then
					if DAMAGE:PlayerCanDamage(player, humanoid) then
						local position	= shot.Position
						local direction	= shot.Directions[index]
						local config	= shot.Config
						
						if direction then
							local ray		= Ray.new(position, direction)
							local distance	= (hit.Position - position).Magnitude
							local ignore	= {}
							
							for _, p in pairs(Players:GetPlayers()) do
								if p.Character then
									table.insert(ignore, p.Character)
								end
							end
							
							--local h	= Raycast(position, direction.Unit * distance, ignore)
							
							if distance <= config.Range then --and (not h) then
								local offset	= ray:Distance(hit.Position)
								if offset < 15 then
									if humanoid.Health > 0 then
										
										shot.Directions[index]	= nil
										local numDir	= 0
										for _, v in pairs(shot.Directions) do
											if v then
												numDir	= numDir + 1
											end
										end
										if numDir == 0 then
											shots[player]	= nil
										end
										
										local down	= humanoid:FindFirstChild("Down")
										local alreadyDowned	= false
										
										if down then
											alreadyDowned	= down.Value
										end
										
										local damage	= DAMAGE:Calculate(shot.Item, hit, position)
										DAMAGE:Damage(humanoid, damage, player)
										
										local otherPlayer	= Players:GetPlayerFromCharacter(humanoid.Parent)
										if otherPlayer then
											REMOTES.HitIndicator:FireClient(otherPlayer, direction, damage)
										end
										
										if humanoid.Health <= 0 then
											for _, part in pairs(humanoid.Parent:GetChildren()) do
												if part:IsA("BasePart") then
													part.Velocity	= direction * config.Damage
												end
											end
											local killDist	= math.floor((position - humanoid.Parent.HumanoidRootPart.Position).Magnitude + 0.5)
											STAT_SCRIPT.FurthestKill:Fire(player, killDist)
											REMOTES.Killfeed:FireAllClients(player.Name, humanoid.Parent.Name, shot.Item.Name, killDist)
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end)
end)

REMOTES.Shoot.OnServerEvent:connect(function(player, item, position, directions)
	if shots[player] then
		shots[player]	= nil
	end
	
	local character	= player.Character
	local rootPart	= character.HumanoidRootPart
	local humanoid	= character.Humanoid
	local down		= humanoid.Down
	local equipped	= character.Equipped
	local items		= character.Items
	
	if item.Parent == items and not down.Value then
		if (rootPart.Position - position).Magnitude < 15 then
			if equipped.Value == item then
				local ammo		= item.Ammo
				local config	= CONFIG:GetConfig(item)
				
				if #directions == config.ShotSize then
					if ammo.Value > 0 then
						ammo.Value	= ammo.Value - 1
						for i, dir in pairs(directions) do
							directions[i]	= dir.Unit
						end
						
						local shot	= {
							Item		= item;
							Config		= config;
							Position	= position;
							Directions	= directions;
						}
						
						shots[player]	= shot
						cancels[item]	= true
						
						for _, other in pairs(Players:GetPlayers()) do
							if other ~= player then
								REMOTES.Effect:FireClient(other, "Shoot", item, position, directions)
							end
						end
					end
				end
			end
		end
	end
end)

Players.PlayerRemoving:connect(function(player)
	if shots[player] then
		shots[player]	= nil
	end
end)
