--//Remote Functions\\--

fire.OnServerEvent:Connect(function(player, mouseHit)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	local weaponAccuracy = Vector3.new(math.random(-accuracy.Value * 2, accuracy.Value * 2), math.random(-accuracy.Value * 2, accuracy.Value * 2), math.random(-accuracy.Value * 2, accuracy.Value * 2))
	
	if humanoid and humanoid ~= 0 then
		local projectile = Instance.new("Part", workspace)
		
			local trail = Instance.new("Trail", projectile)
			trail.FaceCamera = true
			trail.Lifetime = 0.3
			trail.MinLength = 0.15	
			trail.LightEmission = 0.25
						
			local attachment0 = Instance.new("Attachment", projectile)
			attachment0.Position = Vector3.new(0.35, 0, 0)
			attachment0.Name = "Attachment1"
			local attachment1 = Instance.new("Attachment", projectile)
			attachment1.Position = Vector3.new(-0.35, 0, 0)
			attachment1.Name = "Attachment1"
			
			trail.Attachment0 = attachment0
			trail.Attachment1 = attachment1
			
		projectile.Name = "Bullet"
		projectile.BrickColor = BrickColor.new("Smoky gray")
		projectile.Shape = "Ball"
		projectile.Material = Enum.Material.Metal
		projectile.TopSurface = 0
		projectile.BottomSurface = 0
		projectile.Size = Vector3.new(1, 1, 1)	
		projectile.Transparency = 1
		projectile.CFrame = CFrame.new(muzzle.CFrame.p, mouseHit.p)
		projectile.CanCollide = false

			local transparencyPoints = {}
			local startColor = Color3.new(255, 255, 0)
			local endColor = Color3.new(213, 115, 61)
			
			table.insert(transparencyPoints, NumberSequenceKeypoint.new(0, 1))
			table.insert(transparencyPoints, NumberSequenceKeypoint.new(0.25, 0))
			table.insert(transparencyPoints, NumberSequenceKeypoint.new(1, 1))
			
			local determinedTransparency = NumberSequence.new(transparencyPoints)
			local determinedColors = ColorSequence.new(startColor, endColor)
			
			trail.Transparency = determinedTransparency
			trail.Color = determinedColors
		
			local bodyVelocity = Instance.new("BodyVelocity", projectile)
			bodyVelocity.MaxForce = Vector3.new(9e9, 9e9, 9e9)
			bodyVelocity.Velocity = (mouseHit.lookVector * velocity.Value) + weaponAccuracy
			
		debris:AddItem(projectile, 20)
		
		projectile.Touched:Connect(function(hit)
			local eHumanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
			local damage = math.random(minDamage.Value, maxDamage.Value)
			if not eHumanoid and not hit.Anchored and not hit:IsDescendantOf(character) then
				
				projectile:Destroy()
			elseif eHumanoid and eHumanoid ~= humanoid and eHumanoid.Health > 0 and hit ~= projectile then
				
				if hit.Name == "Head" or hit:IsA("Hat") then
					damage = damage * 1.5
				end
				
				local criticalPoint = maxDamage.Value
				DamageAndTagHumanoid(player, eHumanoid, damage) 
				
				if showDamageText then
					DynamicText(damage, criticalPoint, eHumanoid)
				else
				end
				
				projectile:Destroy()
			elseif hit.CanCollide == true and not hit:IsDescendantOf(player.Character) and hit.Anchored == true then
		
				projectile:Destroy()
			end
		end)
		
		handle.Fire:Play()
		muzzleEffect.Visible = true
		muzzleEffect.Rotation = math.random(-360, 360)
		
		delay(0.1, function()
			muzzleEffect.Visible = false
		end)
		
	end
end)

activateSpecial.OnServerEvent:Connect(function(player)
	accuracy.Value, fireRate.Value = accuracy.Value / 2, fireRate.Value / 2
	minDamage.Value, maxDamage.Value = minDamage.Value / 2, maxDamage.Value / 2
	
	spawn(function()
		local chargeSound = Instance.new("Sound", player.PlayerGui)
		chargeSound.Name = "ChargeSound"
		chargeSound.SoundId = "rbxassetid://163619849"
		chargeSound:Play()
		
		chargeSound.Ended:Connect(function() chargeSound:Destroy() end)
		
		local sparkles = Instance.new("Sparkles", handle)
		sparkles.SparkleColor = Color3.fromRGB(248, 248, 248)
		
		local activatedGui = Instance.new("ScreenGui", player.PlayerGui)
		activatedGui.Name = "SpecialActivated"
			local textLabel = Instance.new("TextLabel", activatedGui) 
			textLabel.TextColor3 = Color3.fromRGB(0, 180, 30)
			textLabel.Text = "CHAOS MODE"
			textLabel.Font = Enum.Font.SourceSans
			textLabel.TextScaled = true
			textLabel.TextStrokeTransparency = 0
			textLabel.Size = UDim2.new(0, 300, 0, 50) 
			textLabel.Position = UDim2.new(2.5, 0, 0.15, -10) 
			textLabel.BackgroundTransparency = 1
			textLabel:TweenPosition(UDim2.new(0.5, -(textLabel.Size.X.Offset / 2),  0.1, -10), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 1)
		
		debris:AddItem(sparkles, specialDuration.Value)
		debris:AddItem(chargeSound, 3)
		
		wait(3)
		TextEffects(textLabel, 200, Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 1)
	end)
	
	for i = specialDuration.Value, 0, -1 do
		wait(1)
		print("Special activated: "..i)
	end
	
	accuracy.Value, fireRate.Value = accuracy.Value * 2, fireRate.Value * 2
	minDamage.Value, maxDamage.Value = minDamage.Value * 2, maxDamage.Value * 2
	activateSpecial:FireClient(player)
end)
