------------------------------------------------------------------------------
--------------------[ MAIN PROGRAM ]------------------------------------------
------------------------------------------------------------------------------


function DetonateSmoke()
	local GrenadePos = Main.Position
	
	CreateShockwave(GrenadePos, S.GrenadeEffectRadius)
	
	Sound:Play()
	
	spawn(function()
		for i = 1, math.floor(S.GrenadeEffectRadius * 0.35) do
			local Size = RAND(S.GrenadeEffectRadius * 0.4, S.GrenadeEffectRadius * 0.5)
			local Dist = RAND(0, S.GrenadeEffectRadius - Size)
			local XRot, YRot = RAD(RAND(0, 180, 10)), RAD(RAND(0, 360, 10))
			local RotLV = (CFANG(0, YRot, 0) * CFANG(XRot, 0, 0)).lookVector
			local Pos = GrenadePos + (RotLV * VEC3(Dist, Dist / 2, Dist))
			
			local Smoke = Instance.new("Part")
			Smoke.Transparency = 1
			Smoke.Name = "Smoke"
			Smoke.Anchored = true
			Smoke.CanCollide = false
			Smoke.FormFactor = Enum.FormFactor.Symmetric
			Smoke.Size = VEC3(1, 1, 1)
			Smoke.TopSurface = Enum.SurfaceType.Smooth
			Smoke.BottomSurface = Enum.SurfaceType.Smooth
			
			local Mesh = Instance.new("SpecialMesh")
			Mesh.MeshType = Enum.MeshType.Sphere
			Mesh.Scale = VEC3(Size, Size, Size)
			Mesh.Parent = Smoke
			
			Smoke.Parent = Main.Parent
			Smoke.CFrame = CF(Pos)
			
			spawn(function()
				local Trans = RAND(0.3, 0.5, 0.01)
				for X = 0, 90, 5 do
					Smoke.CFrame = CF(GrenadePos:lerp(Pos, Sine(X)))
					Smoke.Transparency = NumLerp(1, Trans, Sine(X))
					wait()
				end
				
				wait(S.GrenadeEffectTime)
				
				for X = 0, 90, 1 do
					Smoke.CFrame = CF(Pos:lerp(Pos + VEC3(0, 20, 0), 1 - COS(RAD(X))))
					Smoke.Transparency = NumLerp(Trans, 1, Sine(X))
					wait()
				end
				
				Smoke:Destroy()
			end)
			
			wait()
		end
	end)
	
	Main.Transparency = 1
	Main.Anchored = true
	Main.CanCollide = false
	
	wait(S.GrenadeEffectTime + 4)
	Main:Destroy()
end

if S.TimerStartOnHit then
	local Detonated = false
	Main.Touched:connect(function(Obj)
		if IsIgnored(Obj) or Detonated then return end
		Main.Velocity = Main.Velocity / 4
		Detonated = true
		
		wait(S.DetonationTime)
		
		DetonateSmoke()
	end)
else
	spawn(function()
		local Touched = false
		Main.Touched:connect(function(Obj)
			if IsIgnored(Obj) or Touched then return end
			Touched = true
			Main.Velocity = Main.Velocity / 4
		end)
	end)
	wait(S.DetonationTime)
	DetonateSmoke()
end
