local function w(w,k)k=k or{}local u,X,a local y=false repeat u,X,a=Workspace:FindPartOnRayWithIgnoreList(w,k)if u then if u.Parent:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")then y=true elseif u.CanCollide and u.Transparency~=1 then y=true else table.insert(k,u)end else y=true end until y return u,X,a end local function k(w)if w.Splash then REMOTES.Effect:FireAllClients(w.SplashEffect,w.Position,w.SplashRadius)end REMOTES.Projectile:FireAllClients("\075\105\108\108",w.ID)for k,u in pairs(projectiles)do if u==w then table.remove(projectiles,k)break end end if w.Splash then local k=Region3.new(w.Position+Vector3.new(-1,-1,-1)*w.SplashRadius,w.Position+Vector3.new(1,1,1)*w.SplashRadius)local u=Workspace:FindPartsInRegion3WithIgnoreList(k,{Workspace.Effects},100)for k,u in pairs(u)do if u.Name=="\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116"then local k=u.Parent:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if k then local X=Players:GetPlayerFromCharacter(k.Parent)if X==w.Owner or DAMAGE:PlayerCanDamage(w.Owner,k)then if k.Health>0 then local X=(u.Position-w.Position).Magnitude if X<w.SplashRadius then local a=k:FindFirstChild("\068\111\119\110")local y=false if a then y=a.Value end local l=math.cos((X/w.SplashRadius)^2*(math.pi/2))local U=math.ceil(l*w.Damage)local z=Players:GetPlayerFromCharacter(k.Parent)local N=(u.Position-w.Position).Unit if z then REMOTES.HitIndicator:FireClient(z,N,U)end if w.Owner then REMOTES.Hitmarker:FireClient(w.Owner,u.Position,U,k:FindFirstChild("\065\114\109\111\114")and k.Armor.Value>0,false)end DAMAGE:Damage(k,U,w.Owner)if k.Health<=0 then for w,k in pairs(k.Parent:GetChildren())do if k:IsA("\066\097\115\101\080\097\114\116")then k.Velocity=(N*U)*2 end end local X=math.floor((w.StartPosition-u.Position).Magnitude+.5)STAT_SCRIPT.FurthestKill:Fire(w.Owner,X)REMOTES.Killfeed:FireAllClients(w.Owner.Name,k.Parent.Name,w.Model,X)end end end end end end end end end local function u(k,u)k.Velocity=k.Velocity+Vector3.new(0,-k.Gravity*u,0)local X=Ray.new(k.Position,k.Velocity*u)local a,y=w(X,k.Ignore)k.Position=y if a then return false end return true end local function X(w,X,a,y,l)local U=pings[w]and pings[w].Average or 0 local z=CONFIG:GetConfig(X)local N=PROJECTILE:Create(z.Projectile,a)N.Position=y N.Velocity=l.Unit*N.Speed N.Damage=z.Damage N.Owner=w N.StartPosition=y if w.Character then table.insert(N.Ignore,w.Character)end table.insert(projectiles,N)local I=u(N,U)if I then for k,u in pairs(Players:GetPlayers())do if u~=w then REMOTES.Projectile:FireClient(u,"\067\114\101\097\116\101",w,X,a,y,l)end end else k(N)end end


local function Raycast(ray, ignore)
	ignore	= ignore or {}
	
	local hit, position, normal
	
	local success	= false
	repeat
		hit, position, normal	= Workspace:FindPartOnRayWithIgnoreList(ray, ignore)
		
		if hit then
			if hit.Parent:FindFirstChildOfClass("Humanoid") then
				success	= true
			elseif hit.CanCollide and hit.Transparency ~= 1 then
				success	= true
			else
				table.insert(ignore, hit)
			end
		else
			success	= true
		end
	until success
	
	return hit, position, normal
end
	
local function KillProjectile(projectile)
	if projectile.Splash then
		REMOTES.Effect:FireAllClients(projectile.SplashEffect, projectile.Position, projectile.SplashRadius)
	end
	
	REMOTES.Projectile:FireAllClients("Kill", projectile.ID)
	for i, p in pairs(projectiles) do
		if p == projectile then
			table.remove(projectiles, i)
			break
		end
	end
	
	if projectile.Splash then
		local region	= Region3.new(projectile.Position + Vector3.new(-1, -1, -1) * projectile.SplashRadius, projectile.Position + Vector3.new(1, 1, 1) * projectile.SplashRadius)
		local parts		= Workspace:FindPartsInRegion3WithIgnoreList(region, {Workspace.Effects}, 100)
		
		for _, part in pairs(parts) do
			if part.Name == "HumanoidRootPart" then
				local humanoid	= part.Parent:FindFirstChildOfClass("Humanoid")
				
				if humanoid then
					local hitPlayer	= Players:GetPlayerFromCharacter(humanoid.Parent)
					
					if hitPlayer == projectile.Owner or DAMAGE:PlayerCanDamage(projectile.Owner, humanoid) then
						if humanoid.Health > 0 then
							local distance	= (part.Position - projectile.Position).Magnitude
							if distance < projectile.SplashRadius then
								local down	= humanoid:FindFirstChild("Down")
								local alreadyDowned	= false
								
								if down then
									alreadyDowned	= down.Value
								end
								
								local amount	= math.cos((distance / projectile.SplashRadius)^2 * (math.pi / 2))
								local damage	= math.ceil(amount * projectile.Damage)
								local player	= Players:GetPlayerFromCharacter(humanoid.Parent)
								
								local direction	= (part.Position - projectile.Position).Unit
								
								if player then
									REMOTES.HitIndicator:FireClient(player, direction, damage)
								end
								
								if projectile.Owner then
									REMOTES.Hitmarker:FireClient(projectile.Owner, part.Position, damage, humanoid:FindFirstChild("Armor") and humanoid.Armor.Value > 0, false)
								end
								
								DAMAGE:Damage(humanoid, damage, projectile.Owner)
								
								if humanoid.Health <= 0 then
									for _, part in pairs(humanoid.Parent:GetChildren()) do
										if part:IsA("BasePart") then
											part.Velocity	= direction * damage * 2
										end
									end
									local killDist	= math.floor((projectile.StartPosition - part.Position).Magnitude + 0.5)
									STAT_SCRIPT.FurthestKill:Fire(projectile.Owner, killDist)
									REMOTES.Killfeed:FireAllClients(projectile.Owner.Name, humanoid.Parent.Name, projectile.Model, killDist)
								end
							end
						end
					end
				end
			end
		end
	end
end

local function UpdateProjectile(projectile, deltaTime)
	projectile.Velocity	= projectile.Velocity + Vector3.new(0, -projectile.Gravity * deltaTime, 0)
	
	local ray	= Ray.new(projectile.Position, projectile.Velocity * deltaTime)
	local hit, position	= Raycast(ray, projectile.Ignore)
	
	projectile.Position		= position
	--projectile.Debug.CFrame	= CFrame.new(projectile.Position)
	
	if hit then
		return false
	end
	
	return true
end

local function Projectile(player, item, id, position, direction)
	local ping			= pings[player] and pings[player].Average or 0
	local config		= CONFIG:GetConfig(item)
	
	local projectile			= PROJECTILE:Create(config.Projectile, id)
	projectile.Position			= position
	projectile.Velocity			= direction.Unit * projectile.Speed
	projectile.Damage			= config.Damage
	projectile.Owner			= player
	projectile.StartPosition	= position
	
	if player.Character then
		table.insert(projectile.Ignore, player.Character)
	end
	
	--[[local de	= Instance.new("Part")
		de.Anchored		= true
		de.CanCollide	= false
		de.Size			= Vector3.new(1, 1, 1)
		de.Material		= Enum.Material.Neon
		de.Parent		= workspace
		
	projectile.Debug	= de]]
	
	table.insert(projectiles, projectile)
	
	local alive	= UpdateProjectile(projectile, ping)
	
	if alive then
		for _, p in pairs(Players:GetPlayers()) do
			if p ~= player then
				REMOTES.Projectile:FireClient(p, "Create", player, item, id, position, direction)
			end
		end
	else
		KillProjectile(projectile)
	end
end
